question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Babel-node not working with "type": "module" in package.json

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:23 (11 by maintainers)

github_iconTop GitHub Comments

10reactions
bradleycommented, Aug 12, 2020

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 using import 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?

2reactions
nicolo-ribaudocommented, Nov 8, 2020

https://github.com/nicolo-ribaudo/babel-native-esm-demo

my eslint config, my rollup config

I don’t know if those tools support native ESM configs, but Babel does.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found