It is possible in Node.js v14, but you need to use the import operator, rather than the import declaration.
$ node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> let myModule;
undefined
> import("./my-module.js").then(module => { myModule = module });
Promise { <pending> }
> myModule.foo();
"bar"
Answer from diachedelic on Stack OverflowIt is possible in Node.js v14, but you need to use the import operator, rather than the import declaration.
$ node
Welcome to Node.js v14.4.0.
Type ".help" for more information.
> let myModule;
undefined
> import("./my-module.js").then(module => { myModule = module });
Promise { <pending> }
> myModule.foo();
"bar"
This is not currently possible. ES modules are supposed to be imported from the ES module scope, while REPL isn't considered one. This can improve with time because the support of ES modules is experimental. The use of require and import is mutually exclusive in the Node.js module implementation, and REPL already uses require.
Dynamic import is supported in the REPL since Node.js 13. With node --experimental-repl-await, it is:
await import('./right.mjs');
I keep getting this error when I'm trying to import anything in javascript. I have searched up this problem like crazy but can't understand anything. What is package.json??? How do I access it??
The jargon is way too advanced for me. I'm losing my fucking mind trying to fix this.