Abdennour TOUMI Abdennour TOUMI - 1 year ago 304
Node.js Question

Use import keyword without Babel in Node 6

I am wondered why Node.js

node index.js
raises the following error :

(function (exports, require, module, __filename, __dirname) { import * as math from "lib/math";
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:160:18)
at node.js:456:3

Even ,
node -v

keywords is restricted with Babel , even we use Node 6 (6.2.1) ?

Answer Source

ES 2015 Modules (and therefore the import keyword) are not supported by default in Node.js v6.

You can enable experimental (and almost certainly buggy) module support by launching node with the --harmony_modules flag.

A better option might be to use Babel or a similar tool to transpile ES 2015 modules.

Or you can rewrite your code to not use ES 2015 modules at all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download