Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - SyntaxError: Unexpected token import

I don't understand what is wrong. Node v5.6.0 NPM v3.10.6

The code:

function (exports, require, module, __filename, __dirname) {     import express from 'express' }; 

The error:

SyntaxError: Unexpected token import     at exports.runInThisContext (vm.js:53:16)     at Module._compile (module.js:387:25)     at Object.Module._extensions..js (module.js:422:10)     at Module.load (module.js:357:32)     at Function.Module._load (module.js:314:12)     at Function.Module.runMain (module.js:447:10)     at startup (node.js:140:18)     at node.js:1001:3 
like image 409
SofDroid Avatar asked Sep 11 '16 12:09

SofDroid


People also ask

How to fix Unexpected token in Node JS?

The "SyntaxError: Unexpected token import" occurs when we use the ES6 import syntax in a version of Node that doesn't support it. To solve the error, use the require syntax, e.g. const myPackage = require('my-package') or set the type attribute to module in your package. json file.

How do I fix an unexpected token?

This error can occur for a number of reasons, but is typically caused by a typo or incorrect code. Luckily, the SyntaxError: Unexpected token error is relatively easy to fix. In most cases, the error can be resolved by checking the code for accuracy and correcting any mistakes.

What is Syntax error unexpected token?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

What is require () in JavaScript?

1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.


2 Answers

Node 13+ Since Node 13, you can use either the .mjs extension, or set {"type": "module"} in your package.json. You don't need to use the --experimental-modules flag. Modules is now marked as stable in node.js

Node 12 Since Node 12, you can use either the .mjs extension, or set "type": "module" in your package.json. And you need to run node with the --experimental-modules flag.

Node 9 In Node 9, it is enabled behind a flag, and uses the .mjs extension.

node --experimental-modules my-app.mjs 

While import is indeed part of ES6, it is unfortunately not yet supported in NodeJS by default, and has only very recently landed support in browsers.

See browser compat table on MDN and this Node issue.

From James M Snell's Update on ES6 Modules in Node.js (February 2017):

Work is in progress but it is going to take some time — We’re currently looking at around a year at least.

Until support shows up natively (now marked stable in Node 13+), you'll have to continue using classic require statements:

const express = require("express"); 

If you really want to use new ES6/7 features in NodeJS, you can compile it using Babel. Here's an example server.

like image 162
Scimonster Avatar answered Sep 29 '22 08:09

Scimonster


Unfortunately, Node.js doesn't support ES6's import yet.

To accomplish what you're trying to do (import the Express module), this code should suffice

var express = require("express"); 

Also, be sure you have Express installed by running

$ npm install express 

See the Node.js Docs for more information about learning Node.js.

like image 40
baranskistad Avatar answered Sep 29 '22 09:09

baranskistad