Home > Software engineering >  Node.js --experimental-vm-modules command line option vs "type": "module" in pac
Node.js --experimental-vm-modules command line option vs "type": "module" in pac

Time:10-15

I'm currently rewriting a NodeJS 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 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:

CodePudding user response:

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

  • Related