Node error: SyntaxError: Unexpected token import

ES6 imports are a recently introduced feature and the current stable version of Node does not support them yet. Node.js issue tracker has an open issue for this – but until V8 and Node add support for this feature, you will need to use a transpiler (most popular one being babel) to be able to use imports.

For quickly trying out transpilation, babel provides a web based REPL. This one demonstrates your code being transpiled.

The babel project homepage points to the relevant resources for getting started with Babel and integrating it with your development workflow.

For the simplest setup, visit this setup page and select CLI in the Babel built-ins section.

This basically involves three simple steps:

  1. Install babel-cli : npm install --save-dev babel-cli babel-preset-es2015
  2. Create .babelrc configuration file: echo '{ "presets": ["es2015"] }' > .babelrc
  3. Use the installed module to transpile your source code: ./node_modules/.bin/babel src -d lib

The aforementioned setup page also illustrates how to add an npm script to simplify the last step. Alternatively you can integrate babel with your editor or build chain so that your files are automatically compiled on change.

Leave a Comment