Babel-node not working with "type": "module" in package.json
See original GitHub issueI have the following script that I’m running with npx babel-node script.js
let a = 0;
let b = 1;
console.log(a ?? b);
It executes fine with my babel config
{
"plugins": ["@babel/plugin-proposal-nullish-coalescing-operator"]
}
When I add “type”: “module” to my package.json file, the npx babel-node script.js
command fails to run and I get the error SyntaxError: Unexpected token '?'
.
So it seems specifying my package as module, prevents babel-node from loading the babel config?
Issue Analytics
- State:
- Created 4 years ago
- Comments:23 (11 by maintainers)
Top Results From Across the Web
babel-node with typescript throws "Cannot use import ...
I tried type: module but it caused even worse errors. babel.config.js: module.exports = { presets: [ 'module:metro-react ...
Read more >Modules: Packages | Node.js v19.3.0 Documentation
Node.js has two systems for resolving a specifier and loading modules. ... If a package.json file does not have a "type" field, .js...
Read more >Configure Babel
Create a file called babel.config.json with the following content at the root of your project (where the package.json is). ... Check out the...
Read more >type: module package json Code Example
Update to node v12.17.0 or higher, or load the config as commonjs by using the .cjs extension or .js without type="module" set in...
Read more >Documentation - ECMAScript Modules in Node.js - TypeScript
Node.js supports a new setting in package.json called type . ... are interpreted as ES modules or CommonJS modules, and defaults to CommonJS...
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
So what is the suggestion here? My understanding is that we use
@babel/node
for developing a node backend with babel configuration, and then we are supposed to compile the backend using the babel cli for production. In order to keep usingimport
in my node app, I use"type": "module"
for the compiling step… which breaks@bable/node
and therefore development… this seems like an issue to me, shouldn’t this be reopened?https://github.com/nicolo-ribaudo/babel-native-esm-demo
I don’t know if those tools support native ESM configs, but Babel does.