32

Coming from a Python background and being new to Node.js, I've found it surprisingly difficult to interactively try code with Node.js REPL. One key problem that I have faced is with imports:

ES6 style imports won't work on the Node.js REPL and I have to use CommonJS style imports using require. Example: I can't write import rxjs at the Node prompt and have to use require('rxjs'). This makes it harder to copy paste scripts into Node REPL to quickly test them and I have to first convert all ES6 style imports to require imports which feels counterintuitive.

Is there any simple way to be able to use ES6 style imports from the Node.js REPL ?

Like:

$ node
> import 'rxjs';
> import {map} from 'rxjs/operator/map';

I even tried babel-node which doesn't seem to support module imports.

Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99

2 Answers2

14

I'm afraid that the answer is no, or at least not yet. We know that it will be supported in the future, but the ES2015 import and export won't act as in-place substitutions to CommonJS require and module.exports. This is simply because they don't work the same way. You may find more information in this Node.js blog post and this SO question. In particular, one can already run and import standard modules in Node.js, under the flag --experimental-modules (relevant 2ality blog post). However, this feature will still not work from the REPL.

Your idea of using babel-node (which is now called babel-cli) was not that bad: babel implements an interoperable require function, which is kind of a shim that can retrieve modules designed for either type system. Unfortunately, the official website claims lack of support for this functionality in the REPL:

ES6-style module-loading may not function as expected

Due to technical limitations ES6-style module-loading is not fully supported in a babel-node REPL.

Nonetheless, this issue does not apply to production code, which should not rely on the REPL anyway. Instead of using the REPL, you may consider writing a script in a bare-bones project containing babel-cli, and create a script for running the transpiled version:

{
  "name": "my-test-chamber",
  "private": true,
  "scripts": {
    "build": "babel src/main.js -o lib/main.js",
    "start": "npm run build && node lib/main.js"
  },
  "dev-dependencies": {
    "babel-cli": "^6.0.0"
  }
}
E_net4
  • 27,810
  • 13
  • 101
  • 139
  • This solution using `babel-cli` doesn't seem to work in prod. I get `SyntaxError: Unexpected token import` at the import line `import 'rxjs';`. But this answers my question on whether there is a an way way to handle ES6 imports in REPL or not. I hope Node.js introduces a solution for this soon in it's future releases. – Pranjal Mittal Apr 20 '17 at 23:11
  • @PranjalMittal I don't know what `rxjs` is, but you must put the module name between quotes. Then you might be able to put everything in the package inside a namespace with this: `import * as rxks from 'rxks'`. Still, that would not be enough for the REPL to work. – E_net4 Apr 20 '17 at 23:13
  • 1
    Does this need updating? The --experimental-modules flag seems to work great for scripts, but I can't get it to work in the REPL (import gives a syntax error, but require gives a "must use import to load ES module" error). But I'm still using Node 10, it might be different in 11 or 12. – aldel May 13 '19 at 14:29
  • 2
    @aldel Although Node.js currently has experimental support for ESM modules, they still won't work on the REPL, even with `--experimental-modules`. I have updated the answer nevertheless. – E_net4 May 13 '19 at 14:47
  • 3
    So what's the point of using packages? Or rather, since we can use packages, how do we actually go about using them? If import doesn't work, then what does? – claudekennilol Jul 22 '19 at 19:16
  • @claudekenniol Note that ES2015 modules _are_ supported in Node.js, so long as the appropriate flag is included. What still doesn't work at the moment is importing such modules in a REPL environment. – E_net4 Jul 22 '19 at 19:20
0

I found a way to import the ES6 Modules into REPL (I don't remember where):

PS> node
Welcome to Node.js v18.17.0.
Type ".help" for more information.
> const {platform, type} = await import("node:os");
undefined
> console.log('Platform:',platform(), 'Type:', type());
Platform: win32 Type: Windows_NT
undefined
>
> const path = await import("node:path");
undefined
> path.sep
'\\'
FcoJavier99
  • 321
  • 2
  • 8