Is there a strategy on how to migrate from NodeJS CJS + Babel to NodeJS MJS?

Viewed 300

I want to slowly migrate my NodeJS project to a module-style project. And while there are many bits of knowledge on how to use cjs from mjs and wise versa, I have also Babel in the loop, and I'm super confused about what's going on.

At the moment I have a CommonJS node project (that started on node v4). About 80% of the project look like MJS (using imports and exports) thanks to Babel. Now I'm slowly changing small files to native MJS in order to convert the whole project.

I noticed that babel would by default transpile MJS files to JS files. So 1) imports are not working; 2) I don't want to use Babel with MJS because I hope that I can get rid of it eventually. Thus I added **/*.mjs files to the ignored list in the .babelrc file, and fixed file extension to .js, .cjs in the babel/regiter require code.

After this NodeJS keeps complaining that .mjs files have unknown token export. If I run that single .mjs file — everything is good, unless it imports a cjs file that has import in it, and was not transpiled by babel, as I didn't run everything from a root file that registers babel…

Is there a way to have babel transpiling cjs (js) files and keeping mjs files as-is, while node interprets mjs files as native modules?

0 Answers
Related