Use ES6 "import" instruction without transpilers
I have nodejs v8
$ node -v
v8.1.3
and script:
import cmd from "commander";
When trying to run this script:
an "Unexpected token import" error occurs.$ node script.js
/.../script.js:1
(function (exports, require, module, __filename, __dirname) { import cmd from "commander";
^^^^^^
SyntaxError: Unexpected token import
Is there any method for using ES6 modules ("import" statement) without a transpiler?
source to share
The accepted answer is no longer the correct answer. In NodeJS 4+, you can now enable ES modules by installing the package @std/esm
and calling the script with a parameter -r @std/esm
on the command line. For example:
node -r @std/esm index.js
Note that in order for Node to automatically detect if a file should use ESM (so you can use extensions .js
instead of using Michael Jackson's solution .mjs
), you have to add in the package.json
following:
"@std/esm": {
"esm": "js"
},
See standard-things / esm on GitHub for details .
source to share