59

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 core.module.js is:

'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.

Dracontis
  • 4,184
  • 8
  • 35
  • 50
  • 1
    you can use something like `babel`. A related question [link](http://stackoverflow.com/questions/30773756/is-it-okay-to-use-babel-node-in-production) – Sami Sep 02 '15 at 08:20
  • I got the same error message when I dynamically imported a module. This module had an asynchronous method to which I forgot to add `async` in its definition since internally it returned the result with `await`. – alditis May 26 '22 at 15:09

4 Answers4

54

import is a part of ECMAScript 2015 (ES6) standard and as Amit above mentioned it is not currently implemented natively in Nodejs.

So you can use transpiler like babel to run your es6 script

npm install babel

An example based on this answer

app.js

 import {helloworld,printName} from './es6'
 helloworld();
 printName("John");

es6.js

 module.exports = {
    helloworld: function() { console.log('hello world!'); },
    printName: function(name) { console.log(name); }
}

And using require hook in start.js

require("babel/register");
var app = require("./app.js");

And start your app as

node start.js

EDIT The above answer was base on babel v5.8.23. For babel >= v6

Use require hook in start.js as

require('babel-core/register');
require("./app.js");

Also, transformations are not enabled by default. So you will need to install a preset. In this case use es2015

npm install babel-preset-es2015

And use it in a .babelrc file in root folder

{
   "presets": ["es2015"]
}
Community
  • 1
  • 1
Sami
  • 3,926
  • 1
  • 29
  • 42
36

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

Community
  • 1
  • 1
Amit
  • 45,440
  • 9
  • 78
  • 110
  • It's outsource project, so it was build and run on their side somehow. How could I find transpilers? I tried search all over the project for 'babel' or 'transpiler', but haven't found nothing. I could post the full code of core.module, if it could give a clue. I think I can launch it without editing code, but don't know how. – Dracontis Sep 02 '15 at 08:33
  • 4
    So why don't you ask THEM? – Amit Sep 02 '15 at 08:39
  • Also can be they are running it under NodeJS fork which works with SpiderMonkey. – Eugene Obrezkov Sep 02 '15 at 08:40
  • 1
    @Amit, because they said they're busy and refuse to answer questions. If it was that simple, I won't use SO. But I need to figure out myself how to run ot. – Dracontis Sep 02 '15 at 08:51
  • @EugeneObrezkov, for UI part they've used pure Node.js. But I'll try fork. – Dracontis Sep 02 '15 at 08:52
2

I ran into this issue as I manually install any of these tools outside of Visual Studio. But Visual Studio ships with multiple open source command line tools that are used in modern web development workflows. Here’s how you can tell Visual Studio to use the same version that you have manually installed

Go to Tools –> Options –> Projects and Solutions –> External Web Tools

  • Set the global PATH environment variable before the internal path, you can just use the arrows at the top-right to change the order.

or

  • First, find the Node.js installation you already have and use at the command line. By default, Node.js 0.12.7 installs to “C:\Program Files\nodejs”. Add this entry at the top to the path to the node.js directory to force Visual Studio to use that version instead
sree
  • 2,287
  • 28
  • 26
0

This may not be the official answer, but I stumbled here after searching around for the 'unexpected reserved word'. After poking around, I discovered my problem was that, in fact, I just needed to run an npm install after having updated my branch from origin / rebasing. Hope this helps someone else who is furiously reverting their code trying to figure out what they broke! :)

streetlogics
  • 4,640
  • 33
  • 33