MarkForged / nps

NPM Package Scripts -- All the benefits of npm scripts without the cost of a bloated package.json and limits of json
MIT License
6 stars 0 forks source link

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

Open h-unterp opened 2 years ago

h-unterp commented 2 years ago

/Users/hp/.nvm/versions/node/v16.15.0/lib/node_modules/@markforged/nps/dist/bin-utils/index.js:200 var mod = require(modulePath); ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/hp/dev/fiduc_api/functions/package-scripts.js from /Users/hp/.nvm/versions/node/v16.15.0/lib/node_modules/@markforged/nps/dist/bin-utils/index.js not supported. package-scripts.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. Instead rename package-scripts.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/hp/dev/fiduc_api/functions/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

at requireDefaultFromModule (/Users/hp/.nvm/versions/node/v16.15.0/lib/node_modules/@markforged/nps/dist/bin-utils/index.js:200:13)
at attemptModuleRequire (/Users/hp/.nvm/versions/node/v16.15.0/lib/node_modules/@markforged/nps/dist/bin-utils/index.js:186:14)
at loadConfig (/Users/hp/.nvm/versions/node/v16.15.0/lib/node_modules/@markforged/nps/dist/bin-utils/index.js:90:14)
at getPSConfig (/Users/hp/.nvm/versions/node/v16.15.0/lib/node_modules/@markforged/nps/dist/bin-utils/parser.js:281:35)
at parse (/Users/hp/.nvm/versions/node/v16.15.0/lib/node_modules/@markforged/nps/dist/bin-utils/parser.js:128:18)
at Object.<anonymous> (/Users/hp/.nvm/versions/node/v16.15.0/lib/node_modules/@markforged/nps/dist/bin/nps.js:22:33) {

code: 'ERR_REQUIRE_ESM' }

mf-bruce commented 1 year ago

Thanks for the report @h-unterp. Just to clarify, you are encountering this issue when working with packages which have "type": "module" set in package.json - is that correct?

wmertens commented 7 months ago

@mf-bruce Yes this happens in esm projects, and maybe it can be solved by:

anilanar commented 2 months ago

It would be great to add esm support, otherwise esm modules cannot be imported from package-scripts.js. More and more npm packages are migrating to esm and they are distributing esm only.