最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - why export and import error in node.js 9? - Stack Overflow

programmeradmin1浏览0评论

I am learning the javascript not long. I make two files like bellow. app.js:

import * as  Conf from './conf'
console.log(Conf.a)

conf.js:

export let a = 1

And i just run the mand in console:

node app.js

It was err. The err msg:

(function (exports, require, module, __filename, __dirname) { import * 
as Conf from './conf'

SyntaxError: Unexpected token import

Can anyone tell me why? And what is about the javascript and babel and so on. I am not so familiar about the concepts. Thanks.

I am learning the javascript not long. I make two files like bellow. app.js:

import * as  Conf from './conf'
console.log(Conf.a)

conf.js:

export let a = 1

And i just run the mand in console:

node app.js

It was err. The err msg:

(function (exports, require, module, __filename, __dirname) { import * 
as Conf from './conf'

SyntaxError: Unexpected token import

Can anyone tell me why? And what is about the javascript and babel and so on. I am not so familiar about the concepts. Thanks.

Share Improve this question asked Nov 24, 2017 at 8:23 AndyAndy 1611 gold badge3 silver badges10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

There is a ity that each year promote a new javascript norm that are called ECMA 2015 (ES6), ECMA 2016 (ES7), ECMA 2017 (ES8) ...

Each norm describe the functionality javascript must have.

It exists multiple javascript engines in the market:

  • V8 from google (and node)
  • Chakra from microsoft
  • Rhino from mozilla

and so on

Theses engines try to keep up to the norm, but are not 100% pliant.

here you can see a patibility table about the engines and ES6.


The problem you can imagine now, is "Why if I want to use the brand new ES8 right away?". There is two answer: Either you wait for node.js to implement it, either you use a transpiler like Babel.

Babel takes the code you do and transpile it into an older norm (ES5) which is fully patible with node.js.

For example, you want to use import which is an ES6 feature. Babel gonna transform your import into require so node.js can execute it.

It's very usefull and powerfull to use a transpiler so you an use right away the last feature that are improving the productivity of your developper team.


Like @Tuan Anh Tran said, you can use a flag to say to node.js to run it's experimental features, which represent the features it's implementing right now (not particulary safe so for production).


Here is an article about ES6 features.

Here is an article about ES7 features.

Here about ES8 features.

import is ESM keyword and it's in experimental. See nodejs documentation on esm

If you really want to use it, you can either use babel or run your app with --experimental-modules flag and name your file .mjs extension

You need to install Babel or another transpiler to use ES6 import/export in NodeJS.

Babel installation you can find here: https://babeljs.io/docs/setup/#installation

Also you can use NodeJS module loading system.
Docs: https://nodejs/api/modules.html

In your case:

app.js

var conf = require('./conf');
console.log(conf);

conf.js

let a = 1;
module.exports = a;

Or

app.js

var conf = require('./conf');
console.log(conf.a);

conf.js

let a = 1;
exports.a = a;

As the other answers already pointed out, the keywords import and export are experimental.
That's why I'd suggest using require():

Your app.js:

let conf = require("./conf");
console.log(conf.a);

Your conf.js:

let a = 1;
exports.a = a;

node app.js will then print 1 to the console.

发布评论

评论列表(0)

  1. 暂无评论