ESM named exports are not available with "type": "module"
See original GitHub issueA package.json top-level field type with a value of module should make modules load as ES modules.
When I used that with graphql, I expected this to work (it didn’t):
import { version } from "graphql"; // ❌
Importing from index.mjs works fine:
import { version } from "graphql/index.mjs"; // ✅
Shouldn’t ESM named exports work out of the box when Node’s package type is set to module?
Reproduce:
$ node -v
v14.6.0
$ echo '{ "name": "test", "version": "0.0.1", "type": "module" }' > package.json
$ npm i graphql
+ graphql@15.3.0
$ echo 'import { version } from "graphql"; console.log(version);' > test.js
$ node test.js
import { version } from "graphql"; console.log(version);
^^^^^^^
SyntaxError: The requested module 'graphql' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'graphql';
const { version } = pkg;
at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:137:5)
at async Loader.import (internal/modules/esm/loader.js:162:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:24
- Comments:37
Top Results From Across the Web
Named export 'Types' not found. The requested module ...
Named export 'Types' not found. The requested module 'mongoose' is a CommonJS module, which may not support all module.exports as named exports.
Read more >Modules: Packages | Node.js v19.3.0 Documentation
Such modules are passed through the cjs-module-lexer to try to identify named exports, which are available if they can be determined through static...
Read more >ECMAScript Modules - webpack
The export keyword allows to expose things from an ESM to other modules: ... "default" export can be imported from non-ESM. Named exports...
Read more >How to write CommonJS exports that can be name-imported ...
When importing CommonJS modules, the module.exports object is provided as the default export. Named exports may be available, provided by static ...
Read more >The requested module does not provide an export named in JS
The error "The requested module does not provide an export named" occurs when mixing up default and named ES6 module imports and exports....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The way I resolved is to add
to the [rules] section.
hi @kettanaito looks like webpack 5 doesn’t resolve it anymore. I’m getting this error: