ESM file extension
Diluka opened this issue ยท 9 comments
internal/modules/cjs/loader.js:1015
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /path/to/proj/node_modules/quick-lru/index.js
require() of ES modules is not supported.
require() of /path/to/proj/node_modules/quick-lru/index.js from /path/to/proj/dist/src/ih-user/ih-user.guard.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 index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /path/to/proj/node_modules/quick-lru/package.json.
like this?
{
"main": "index.cjs.js",
"module": "index.esm.js"
}
Please read the release notes.
so this lib will no longer available in express
projects? a lot of nodejs
packages are only support commonjs
ESM can import CommonJS, so you can convert your project to ESM, while still importing CommonJS dependencies. I would recommend reading up on ESM.
I tried, and give up. There are some file extension naming issue with typescript
in nodejs
. It is not easy to change a nest.js
project from commonjs
to esm
.
end up here:
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/path/to/proj/dist/src/bootstrap.module' imported from /path/to/proj/dist/src/main.js
at finalizeResolution (internal/modules/esm/resolve.js:271:11)
at moduleResolve (internal/modules/esm/resolve.js:694:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:784:11)
at Loader.resolve (internal/modules/esm/loader.js:100:40)
at Loader.getModuleJob (internal/modules/esm/loader.js:246:28)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:47:40)
at link (internal/modules/esm/module_job.js:46:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
@sindresorhus Real problems with ts-node setup.
Today I tried to use this package in NextJS, but got exception about esm. NextJS provides esm support via build system on top of webpack, but it does not expose it's internals. Attempts to configure it to work with esm modules only are not acceptable because of old browsers support.
@zaadevofc try https://www.npmjs.com/package/@alloc/quick-lru backport new featue to commonjs version