Search code examples
node.jsmoduleecmascript-6transpiler

Use ES6 'import' statement without transpilers


I have nodejs v8

$ node -v
v8.1.3

and a script:

import cmd from "commander";

There is an error "Unexpected token import" when I try to launch this script:

$ node script.js
/.../script.js:1
(function (exports, require, module, __filename, __dirname) { import cmd from "commander";
                                                              ^^^^^^

    SyntaxError: Unexpected token import

Does any method exist to use ES6 modules ("import" statement) without transpiler?


Solution

  • Without using transpiler, you have to use require:

    const cmd = require('commander');