sindresorhus / read-package-up

Read the closest package.json file
MIT License
258 stars 18 forks source link

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /root/workspace/node_modules/read-pkg-up/index.js #18

Closed khteh closed 3 years ago

khteh commented 3 years ago

Using node:latest bumps into this error:

./node_modules/.bin/nyc --reporter=lcov --reporter=text ./node_modules/.bin/mocha tests --timeout 10000 --recursive --check-leaks --exit  --reporter mocha-junit-reporter --reporter-options mochaFile=test_reports/mocha/test-results.xml
node:internal/modules/cjs/loader:1109
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /root/workspace/node_modules/read-pkg-up/index.js
require() of ES modules is not supported.
require() of /root/workspace/node_modules/read-pkg-up/index.js from /root/workspace/node_modules/test-exclude/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /root/workspace/node_modules/read-pkg-up/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /root/workspace/node_modules/read-pkg-up/package.json.

    at new NodeError (node:internal/errors:329:5)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1109:13)
    at Module.load (node:internal/modules/cjs/loader:972:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:996:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at Object.<anonymous> (/root/workspace/node_modules/test-exclude/index.js:4:19)
    at Module._compile (node:internal/modules/cjs/loader:1092:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1121:10)
    at Module.load (node:internal/modules/cjs/loader:972:32) {
  code: 'ERR_REQUIRE_ESM'
}

Exited with code exit status 1

https://stackoverflow.com/questions/66938218/node-js-read-pkg-up-bumps-into-error-err-require-esm-must-use-import-to-load

sindresorhus commented 3 years ago

Duplicate of #17

khteh commented 3 years ago

I don't understand how reading your README could solve the issue!

/open

papb commented 3 years ago

Not the readme, the release notes. Which in turn bring you to this page.

alvis commented 3 years ago

@sindresorhus Would you consider releasing a hybrid version (ESM + common js)? I use typescript but I have to release my project in hybrid mode for compatibility. But v8 breaks the possibility because the code can't call read-pkg-up in the commonjs way anymore.