Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 883
Dracontis Avatar asked Sep 02 '15 07:09

Dracontis


People also ask

How do I fix unexpected reserved word await?

The "unexpected reserved word await" error occurs when the await keyword is used inside of a function that was not marked as async . To use the await keyword inside of a function, mark the directly enclosing function as async .

How do you fix uncaught ReferenceError require is not defined?

To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.


2 Answers

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"] } 
like image 100
Sami Avatar answered Sep 21 '22 12:09

Sami


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

like image 31
Amit Avatar answered Sep 23 '22 12:09

Amit