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');
Videos
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.
You cannot use static import statements (e.g. import someModule from "some-module"), at the moment. I'm not aware of any efforts/tickets/pull requests/intents to change that.
You can use import() syntax to load modules! This returns a promise. So for example you can create a variable someModule, start importing, & after done importing, set someModule to that module:
let someModule
import("some-module")
.then( loaded=> someModule= loaded)
Or you can directly use the import in your promise handler:
import("some-module").then(someModule => someModule.default())
For more complex examples, you might want to use an async Immediately Invoked Function Expression, so you can use await syntax:
(async function(){
// since we are in an async function we can use 'await' here:
let someModule = await import("some-module")
console.log(someModule.default())
})()
last, if you start Node.JS with the --experimental-repl-await flag, you can use async directly from the repl & drop the async immediately invoked function:
let someModule = await import("some-module")
// because you have already 'await'ed
// you may immediately use someModule,
// whereas previously was not "loaded"
console.log(someModule.default())
Given the static nature of import statements and that the engine needs to know all static imports (before processing any non-import related code). You can see how the import statement really isn't compatible with interactive REPL support.
For example, import statements are allowed in the middle or end of a file, but they're hoisted and processed before "non-static-import related code".
What should the REPL do if you type a static import statement at the end of your REPL session?
It can't easily go back and rerun all your prior commands in light of this potentially fundamental change!
I didn't think about that until reading this. Thanks Kevin Qian who also has ideas.