#Error [ERR_REQUIRE_ESM]: require() of ES Module

44 messages · Page 1 of 1 (latest)

summer skiff
#

Having this issue trying to import a lib

#

More context: #ts-discussion message

#

Tsconfig:

#
{
  "compilerOptions": {
    "rootDir": ".",
    "outDir": "build",
    "skipLibCheck": true,
    "module": "commonjs",
    "target": "es2022",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "importHelpers": true,
    "strictPropertyInitialization": false
  },
  "include": [
    "src/**/*.ts",
    "src/locales/*.json"
  ],
  "exclude": ["/.github", "./build", "./node_modules"]
}
#

Not even dynamic imports are working

prisma brook
#

@summer skiff If you're trying to load an external module that requires ESM from CJS, it just doesn't work.

summer skiff
#

right?

#

But if I do so, I start getting this error from ts-node: TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

#

so I use ts-node-esm

#

now it gives me ReferenceError: exports is not defined in ES module scope

prisma brook
summer skiff
summer skiff
#

it means I have to build then run it?

prisma brook
#

No? The point of ts-node is to not build then run.

summer skiff
#

I know

#

it is the same thing as ts-node-esm

#

But then I get the error I told you about

#

ReferenceError: exports is not defined in ES module scope

prisma brook
#

Sounds like some code is still running as CJS.

summer skiff
#
{
  "compilerOptions": {
    "rootDir": ".",
    "outDir": "build",
    "skipLibCheck": true,
    "module": "NodeNext",
    "target": "es2022",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "importHelpers": true,
    "strictPropertyInitialization": false
  },
  "include": [
    "src/**/*.ts",
    "src/locales/*.json"
  ],
  "exclude": ["/.github", "./build", "./node_modules"]
}
prisma brook
#

Yeah, I saw that the first time;

summer skiff
#

and I have "type": "module", on packages.json

#

I also updated every local import to use the .js and index.js

prisma brook
#

Sounds right to me, not sure what else to tell you - can't really debug with nothing but a tsconfig .

summer skiff
#

I can commit and send you the repo if you want

#

but this is just weird

#

oh

#

seems like I was missing modeuleResolution

#

modeuleResolution also needed to be set to nodenext

prisma brook
#

May want to double check that whole list make sure there aren't any other surprises.

summer skiff
#

Yeah now it runs but facing new issues

#

Suddenly facing this Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file and data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'e:'

#

And the stacktrace is useless

summer skiff
#

@prisma brook Yeah I can't seem to solve this, this is a Bot so I have a lot of commands and these commands were loaded dynamically

#

Now I keep getting this: CustomError: ERR_INVALID_MODULE_SPECIFIER ..\commands\help.ts is not a valid package name E:\Projetos\.js\spear-ts\src\libs\spear.ts

prisma brook
#

I guess I'd try running without ts-node and see if things work, and then if they do, try #ts-node .

#

Would be good to identify if ts-node is part of the issue here or if there's still issues with your setup.

summer skiff
prisma brook
#

Ah, I always recommend against loading commands dynamically.