How to import an ES module in Node.js REPL?

后端 未结 3 1982
离开以前
离开以前 2020-12-09 09:28

I have an ES6 module right.mjs. Executing it as a parameter to node works well:

$ node --version
v8.10.0
$ node --experimental-modu         


        
3条回答
  •  无人及你
    2020-12-09 10:13

    It is possible in Node v14, but you need to use the import function, rather than the import statement.

    $ 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 {  }
    > myModule.foo();
    "bar"
    

提交回复
热议问题