#Error [ERR_REQUIRE_ESM]: require() of ES Module
44 messages · Page 1 of 1 (latest)
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
@summer skiff If you're trying to load an external module that requires ESM from CJS, it just doesn't work.
Which means that I am supposed to use type: module and nodenext
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
You don't need to switch away from ts-node to use ESM: https://github.com/TypeStrong/ts-node/tree/main/esm-usage-example
I followed this and if I use ts-node-esm I get ReferenceError: exports is not defined in ES module scope
and if I try to do this
it means I have to build then run it?
No? The point of ts-node is to not build then run.
According to the docs, you should be able to just pass a --esm flag when running ts-node actually: https://github.com/TypeStrong/ts-node#esm
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
Sounds like some code is still running as CJS.
{
"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"]
}
Yeah, I saw that the first time;
and I have "type": "module", on packages.json
I also updated every local import to use the .js and index.js
Sounds right to me, not sure what else to tell you - can't really debug with nothing but a tsconfig .
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
Ah yeah, that was listed on the list in the gist: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#how-can-i-make-my-typescript-project-output-esm
May want to double check that whole list make sure there aren't any other surprises.
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
@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
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.
I had to completely rewrite the way my bot loads commands dynamically.
Ah, I always recommend against loading commands dynamically.