Import ES6 style into Node.js REPL

Coming from the Python background and being new to Node.js, it was unexpectedly difficult for me to interactively try the code with Node.js REPL. One of the key issues that I have encountered is imports:

Importing ES6 styles will not work on Node.js REPL, and I have to use JJJJ style style import using require. Example: I cannot write import rxjsat the Node prompt and use require('rxjs'). This makes it difficult to copy paste scripts into the Node REPL in order to quickly test them, and I must first convert all the ES6 styles it imports to require imports that seem counterintuitive.

Is there any easy way to use ES6 style import from Node.js REPL?

how

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

I even tried babel-node, which does not seem to support importing modules.

+12
source share
1 answer

, - , , , . , , import export ES2015 , CommonJS require module.exports. , . Node.js . , Node.js --experimental-modules ( 2ality). - REPL.

babel-node ( babel-cli) : babel require, , , . , REPL:

ES6

- ES6 Babel- REPL.

, , REPL. , REPL, , babel-cli, :

{
  "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"
  }
}
+5
source

Source: https://habr.com/ru/post/1675307/


All Articles