Reputation: 1854
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?
Upvotes: 3
Views: 1177
Reputation: 1011
The accepted answer is no longer the correct answer. In NodeJS 4+, you can now enable ES modules by installing the @std/esm
package and calling your script with the -r @std/esm
option on the command line. For example:
node -r @std/esm index.js
Note that in order to have Node auto-detect if a file should use ESM (to allow you to use .js
file extensions instead of using the Michael Jackson solution, .mjs
), you'll have to add to your package.json
the following:
"@std/esm": {
"esm": "js"
},
See full details at standard-things/esm on GitHub.
Upvotes: 10
Reputation: 10366
Without using transpiler, you have to use require
:
const cmd = require('commander');
Upvotes: 1