Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 style imports in Node.js REPL

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.

like image 854
Pranjal Mittal Avatar asked Apr 20 '17 22:04

Pranjal Mittal


People also ask

Does node js support ES6 import?

Node js doesn't support ES6 import directly. If we try to use import for importing modules directly in node js it will throw out the error. For example, if we try to import express module by writing import express from 'express' node js will throw an error as follows: Node has experimental support for ES modules.

How do I import a file into node REPL?

You can type ". load " (note the space) into the REPL and drag/drop the file into the Terminal from the Finder to add the correct path to your command.

What is ES6 in node JS?

ES6, ECMAScript 6 or ES2015 is the latest specification for JavaScript which introduces some syntactic sugar to the language. It's a big update to the language and introduces a lot of new features. More details on Node and ES6 can be found on their site https://nodejs.org/en/docs/es6/


1 Answers

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"   } } 
like image 188
E_net4 stands with Ukraine Avatar answered Sep 28 '22 08:09

E_net4 stands with Ukraine