Dracontis Dracontis - 1 year ago 272
Javascript Question

unexpected reserved word import in node.js

I'm trying to run node.js backend server. I've received error

unexpected reserved word
on import in Node.js file.

The lines in file

'use strict';
import lodashMixins from './lodashMixins.js'
... other imports and configurations ...

I launch simple command:
node core.module.js

It's not uncommon error, but usually it happens with other libraries. I haven't seen solution for Node.js. How should I fix this? I'm using Windows Server.

Edit: I've find out that it's ES6, but how could I launch it? It looks like backend for the application, but I have no idea what command should I use to launch it without errors.

Answer Source

The import keyword is part of the modules feature in ECMAScript 2015, along with export and a few other specifications.

It is currently not implemented natively in NodeJS, even on the lastest version (v0.12.7), nor is it supported in the ES2015 "friendlier" fork iojs.

You will need to use a transpiler to get that to work.

[edit] it's still unsupported in the latest version (v5.8) despite the existence of an --harmony_modules flag, which does nothing. Your best run is to use babel, as explained here and here

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