Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 import statements not working in Node v8.4.0

Tags:

node.js

I'm using the latest version of Node.js that is v8.4.0. However, in the import and export statements, I'm getting errors:

import express from 'express';
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:537:28)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:598:3

What Node.js version should I install in order to work these ES6 codes?

  • OS - Ubuntu 17.04
  • node -v: v8.4.0
  • npm -v: 5.3.0
like image 575
Gijo Varghese Avatar asked Sep 12 '17 12:09

Gijo Varghese


People also ask

Does Nodejs support ES6 import?

Node js doesn't support ES6 import directly. If we try to use import for importing modules directly in node js it will throw out the error. For example, if we try to import express module by writing import express from 'express' node js will throw an error as follows: Node has experimental support for ES modules.

Why I Cannot use import in node?

The "SyntaxError: Cannot use import statement outside a module" occurs when we use the ES6 Modules syntax in a script that was not loaded as a module. To solve the error, set the type attribute to module when loading a script, or in your package. json for Node.

What version of Node supports ES6?

While support for ES6 modules arrived as an experimental feature in Node. js 8.5, there are two ways to use these modules on earlier Node. js implementations. One method is to use the Babel transpiler to rewrite ES6 code so it can execute on older Node.

Can you use import statements in node?

If you have used other JavaScript frameworks like React or Vue. js, then you know that these frameworks use the “import” keyword for importing files and modules instead of the “require” keyword — used by Node. js. But if you want to use the “import” statement in your Node.


1 Answers

One way that I have worked around this issue...

Install babel stuff for the project:

$ npm install babel-register babel-preset-es2015 --save-dev

Create an index.js file that is the main entry point into the app:

// index.js 
// by requiring `babel/register`, all of our successive `require`s will be Babel'd
require('babel-register')({
   presets: [ 'es2015' ]
});

require('./server');

Then, create a file called server.js that will have your normal index code:

// server.js
import express from 'express';

var app = express()

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(3000, () => console.log('Example app listening on port 3000!'))

And run:

$ node index.js 
like image 199
Chris Livdahl Avatar answered Oct 23 '22 11:10

Chris Livdahl