Author -  Sai gowtham

How to resolve the SyntaxError: Unexpected token import Node.js

In this tutorial, we are going to learn about how to resolve the unexpected token import error in Node.js.

When we use es6 modules import/export statements in Node.js. It gives a following error in the terminal because node is currently not supporting the es6 modules by default.

app.js
import express from "express";

const app = express();

app.get("/hello", (req,res)=>{
   res.send("hello")
})

app.listen(3000, ()=> console.log("server is running"));

Output:

import express from "express";
^^^^^^

SyntaxError: SyntaxError: Unexpected token import
    at Module._compile (internal/modules/cjs/loader.js:891:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11

To fix this error, we can use the commonjs modules require() function and module.exports instead of es6 modules.

app.js
const express =  require("express");

const app = express();

app.get("/hello", (req,res)=>{
   res.send("hello")
})

app.listen(3000, ()=> console.log("server is running"));

Or you can use the es6 modules by adding a --experimental-modules flag to the node command and set "type":"module" to the package.json file.

node --experimental-modules app.js

This above command can be used in node versions 8, 9, 10, 11, 12.

For node version >=13 you can use the es6 modules by setting a "type":"module" to the package.json file. There is no need of --experimental-modules flag in node command.

package.json
{
  "name": "my-app",
  "version": "1.0.0",
  "description": "",
  "type": "module",  "scripts": {
    "start": "node app.js"
  }
}

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY