Closed gustawdaniel closed 1 year ago
If you have the same problem I described currently working solution:
but hope it will be fixed here to simplify usage with ts-node
default config.
@SBoudrias i found your comment in https://github.com/SBoudrias/Inquirer.js/issues/1129
I understand your statement, but please think about providing more detailed docs in README.md assuming that many developers do not know how to configure his project for esm.
Duplicate of #1129.
This relates to basic knowledge of ESM. The error message already tells developers what the problem is.
I think developers should read the documentation of ts-node
before use. They have already introduced how to deal with ESM.
I am using
ts-node
and see➜ npx ts-node src/index.ts I need list files in directory /home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/index.js:851 return old(m, filename); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module /home/daniel/pro/gpt-cli/node_modules/@inquirer/confirm/dist/index.js from /home/daniel/pro/gpt-cli/src/index.ts not supported. Instead change the require of index.js in /home/daniel/pro/gpt-cli/src/index.ts to a dynamic import() which is available in all CommonJS modules. at require.extensions.<computed> [as .js] (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/index.js:851:20) at Object.<anonymous> (/home/daniel/pro/gpt-cli/src/index.ts:18:35) at m._compile (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/index.js:857:29) at require.extensions.<computed> [as .ts] (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/index.js:859:16) at phase4 (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/bin.js:466:20) at bootstrap (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/bin.js:54:12) at main (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/bin.js:33:12) at Object.<anonymous> (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/bin.js:579:5) { code: 'ERR_REQUIRE_ESM' }
The error "[ERR_REQUIRE_ESM]: require() of ES Module not supported. Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules" occurs because a package you are importing has been converted to an ESM-only package
, which means that the package cannot be imported with require()
anymore.
"type": "module"
from package.json if it's addedtsconfig.json
under the compilerOptions
Set module property to CommonJS module: "CommonJS"
and moduleResolution: "Node"
This solution was the one I used.
"type": "module"
to package.jsonnpm i -g ts-node
tsconfig.json
and add the following:{
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node",
/* ... your props ... */
},
"ts-node": {
"esm": true
}
}
ts-node {filename}.ts
or if it is installed locally run npx ts-node {filename}.ts
.js
(only the files belonging to your project)//Bad
import { random } from './utils'
//Good
import { random } from './utils.js'
I am using
ts-node
and see➜ npx ts-node src/index.ts I need list files in directory /home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/index.js:851 return old(m, filename); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module /home/daniel/pro/gpt-cli/node_modules/@inquirer/confirm/dist/index.js from /home/daniel/pro/gpt-cli/src/index.ts not supported. Instead change the require of index.js in /home/daniel/pro/gpt-cli/src/index.ts to a dynamic import() which is available in all CommonJS modules. at require.extensions.<computed> [as .js] (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/index.js:851:20) at Object.<anonymous> (/home/daniel/pro/gpt-cli/src/index.ts:18:35) at m._compile (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/index.js:857:29) at require.extensions.<computed> [as .ts] (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/index.js:859:16) at phase4 (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/bin.js:466:20) at bootstrap (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/bin.js:54:12) at main (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/bin.js:33:12) at Object.<anonymous> (/home/daniel/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/dist/bin.js:579:5) { code: 'ERR_REQUIRE_ESM' }
Explanation of the problem.
The error "[ERR_REQUIRE_ESM]: require() of ES Module not supported. Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules" occurs because a package you are importing has been converted to an
ESM-only package
, which means that the package cannot be imported withrequire()
anymore.First solution
- Remove
"type": "module"
from package.json if it's added- In
tsconfig.json
under thecompilerOptions
Set module property to CommonJSmodule: "CommonJS"
andmoduleResolution: "Node"
Second solution
This solution was the one I used.
- Add
"type": "module"
to package.json- Install ts-node
npm i -g ts-node
- Go to
tsconfig.json
and add the following:{ "compilerOptions": { "module": "ESNext", "moduleResolution": "Node", /* ... your props ... */ }, "ts-node": { "esm": true } }
- Run
ts-node {filename}.ts
or if it is installed locally runnpx ts-node {filename}.ts
- Incorporating this change requires all imports to carry
.js
(only the files belonging to your project)//Bad import { random } from './utils' //Good import { random } from './utils.js'
With this the error has to be solved. If you found another way to solve the problem, you can reply to this comment to help others. 🫶
Second solution worked for me. Thanks.
Just wanted to share that the readme in ts-node provides a clear explanation of this error. https://github.com/TypeStrong/ts-node#err_require_esm
this tsconfig.json
file works for me.
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "ESNext",
"esModuleInterop": true
},
"ts-node": {
"esm": true
}
}
Note: don't forget to make "type":"module"
in your package.json
file.
{
"name": "nodejs-cli",
"version": "1.0.0",
"description": "description",
"type": "module",
"scripts": {
"dev": "nodemon cli.ts"
},
"license": "MIT",
"dependencies": {
"inquirer": "^9.2.12"
},
"devDependencies": {
"@types/inquirer": "^9.0.7",
"@types/node": "^20.10.7",
"nodemon": "^3.0.2",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}
Not working for me
{ "private": true, "scripts": { "build": "node ./build.js && nuxi build", "dev": "node ./build.js && nuxi dev", "generate": "node ./build.js && node ./sitemap.js && nuxi generate", "start": "npx serve .output/public", "preview": "nuxi preview", "slicemachine": "start-slicemachine --port 9999" }, "devDependencies": { "@nuxtjs/prismic": "^3.3.2", "@prismicio/helpers": "^2.3.9", "@slicemachine/adapter-nuxt": "^0.3.42", "@vueuse/core": "^10.4.1", "@vueuse/nuxt": "^10.4.1", "consola": "^3.2.3", "node-fetch": "^2.7.0", "nuxt": "^3.7.4", "sass": "^1.68.0", "sass-loader": "^14.2.1", "slice-machine-ui": "2.2.1" }, "dependencies": { "@gtm-support/vue-gtm": "^3.0.1", "@nuxt/webpack-builder": "^3.7.4", "@pinia/nuxt": "^0.5.1", "@splidejs/vue-splide": "^0.6.12", "aos": "^3.0.0-beta.6", "cheerio": "^1.0.0-rc.12", "micromodal": "^0.4.10", "mitt": "^3.0.1", "object-fit-images": "^3.2.4", "pinia": "^2.1.6", "vanilla-lazyload": "^19.1.3", "vee-validate": "^4.11.7", "vue-select": "^4.0.0-beta.6", "yup": "^1.3.2" } }
node_modules\@prismicio\client\dist\helpers\unstable_htmlAsRichText.cjs:3 const rehypeParse = require("rehype-parse"); ^
Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules\rehype-parse\index.js from C:\node_modules\@prismicio\client\dist\helpers\unstable_htmlAsRichText.cjs not supported.
Instead change the require of index.js in C:\sc-prismic-anthony\sc-web-2022\node_modules\@prismicio\client\dist\helpers\unstable_htmlAsRichText.cjs to a dynamic import() which is available in all CommonJS modules.
at Module.
Node.js v19.7.0 error Command failed with exit code 1.
@rajender-rw sorry but this isn't related to inquirer. It's not inquirer
or one of our dependencies.
If you read your trace, you'll see the error comes from rehype-parse
node_modules@prismicio\client\dist\helpers\unstable_htmlAsRichText.cjs:3
const rehypeParse = require("rehype-parse");
^
I guess coming from prismicio
.
I am using
ts-node
and see