Friday, 5 June 2020

ES6 style modules in NodeJS

In Node v12 I was using ES6/ESM imports and importing JSON by calling node with these flags:node --experimental-modules --experimental-json-modules --es-module-specifier-resolution=node index.jsThis let me do imports like this:import myFunc from "./lib/myFunc"Now, in Node v14, we apparently no longer need the experimental-modules flag. However, when I run node index.js I get an error telling me that my imports need the .js extension like this:import myFunc from "./lib/myFunc.js"Is this the intended behaviour? If so, why have they suddenly added the need for the .js extension when previously it wasn't required. The exact error I get is:Cannot find module 'c:\xxx\lib\myFunc' imported from c:\xxx\index.js Did you mean to import ../lib/myFunc.js?Can someone tell me what's going on? I don't really want to have to search my entire app and add .js to every module import just to test if this even works.

Submitted June 05, 2020 at 02:22PM by niwork-account

No comments:

Post a Comment