Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 import and export are not supported in Node.js

I'm building a project using WebStorm 2016.2.2. I use version 6.6.0 of Node.js. I have set the JavaScript version to ES6, but I get "unexpected token" errors for both import and export keywords. I don't get errors for Lambda expressions BTW.

Why do I get this error? Am I doing something wrong or Node just does not support import and export without Babel (even though I have set the JavaScript version to ES6)?

like image 920
Alon Avatar asked Oct 06 '16 16:10

Alon


People also ask

Does Nodejs 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.

Does Nodejs support ECMAScript?

Node.js has two module systems: CommonJS modules and ECMAScript modules. Authors can tell Node.js to use the ECMAScript modules loader via the .mjs file extension, the package.json "type" field, or the --input-type flag.

Does node 14 support import?

Node 14 introduces ECMAScript modules — no more transpiling, no more difficulties with import and export.


2 Answers

Node.js supports CommonJS modules, however support for ECMAScript modules is currently being designed.

In the meantime, you'll have to use Babel to transform ECMAScript modules to CommonJS. Try using a minimal preset for Node.js builds, like babel-preset-node6.

like image 189
Filip Dupanović Avatar answered Oct 05 '22 23:10

Filip Dupanović


es6 does support import and export, node 6 does not support es6 import/export up till now as far as I know.

This question can be helpful: NodeJS plans to support import/export es6 (es2015) modules

like image 43
Stavros Zavrakas Avatar answered Oct 05 '22 23:10

Stavros Zavrakas