Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js --experimental-vm-modules command line option vs "type": "module" in package.json

I'm currently rewriting a Node.js project from a messy combination of CommonJS and Babel-transpiled ES Modules to be completely ES Module based.

In the process, I've become a bit confused by how Node handles these things. I've tried to read up on it, but I seem to be missing one final piece in my understanding.

I was assuming I would no longer need to use Babel to transpile the ES Modules. This seems to be correct. However, when I run my (Jest) tests, I still need to use the --experimental-vm-modules Node.js flag to make everything work.

My question is: what does that flag do? Reading the documentation, this seems to enable ES Modules, but didn't I already enable ES Modules by specifying "type": "module" in the package.json file?

Relevant links:

  • https://nodejs.org/api/esm.html#enabling
  • https://nodejs.org/api/cli.html#--experimental-vm-modules
  • https://jestjs.io/docs/ecmascript-modules
like image 649
Peter Avatar asked Dec 09 '25 17:12

Peter


1 Answers

Specifying type: "module" in package.json just telling library authors your source code is based on ESM.

For jest, you need to use experimental-vm-modules because the node api jest uses to enable ESM support is still not stable as of node 18.x

https://jestjs.io/docs/ecmascript-modules

like image 196
ZEE Avatar answered Dec 11 '25 06:12

ZEE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!