How To Solve “Unexpected Token Import Syntaxerror” In Node.js?

Unexpected token import syntaxerror in node.js

When working with a NodeJS project, you use the import keyword and get the error message “unexpected token import syntaxerror” in node.js. In this article, I will show you some solutions.

Why does the error occur?

The reason we are getting the error message unexpected token import syntaxerror in node.js is that we are using ES6 import syntax on an unsupported version of NodeJS.

Assuming that you have a file named ‘getSalary.js‘ and the third-party module: express installed on your machine using the command npm install express. Here is a specific example of an error.

Your ‘getSalary.js’ file:

module.exports =
{
    getDevSalary: function(base,timeKeeping){
    return base*timeKeeping*0.5
    },

    getTesterSalary: function(base,timeKeeping){
        return base*timeKeeping*0.4
    }
} 

Then we will import them into the main.js file.

In main.js

import express from ‘express’
import { getDevSalary, getTesterSalary } from './getSalary.js'

Output:

SyntaxError: Cannot use import statement outside a module

Solving the error “unexpected token import syntaxerror” in node.js

Method 1 – Using the require() function

NodeJS provides us with the require() function to be able to import this module into another.

const express = require('express')
const {getDevSalary, getTesterSalary} = require('./getSalary.js')

const res1 = getDevSalary(5,30)
const res2 = getTesterSalary(4,30)

console.log(res1)
console.log(res2)

Output:

75
48

As you can see, the error didn’t happen anymore. This command is equivalent to using import in ES6.

Method 2 – Edit the package.json file

If you still want to use the ES6 syntax, open the package.json file and set the ‘type‘ attribute to ‘module‘.

Your package.json should be like this:

{
  "name": "index",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {},
  "keywords": [],
  "author": "",
  "license": "ISC",
    ……
}

By adding the attribute “type”: “module” , you can use ES6 modules. You can now use the import statement.

import express from ‘express’
import { getDevSalary, getTesterSalary} from './getSalary.js'
const res1 = getDevSalary(5,30)
const res2 = getTesterSalary(4,30)

console.log(res1)
console.log(res2)

Output:

75
48

Method 3 – Using the esm module

First, you need to install ems module on your computer:

npm install esm

Now you can execute the main.js file (still using the import statement) above by running the following command in the terminal:

node -r esm main.js

You will also see the results on the console window as 75 and 48.

Method 4 – Using the mjs extension

Alternatively, if you don’t want to edit your package.json file, write the program as an mjs extension. You can use the ES6 import statement normally.

In main.mjs

import express from ‘express’
import { getDevSalary, getTesterSalary} from './getSalary.js'
const res1 = getDevSalary(5,30)
const res2 = getTesterSalary(4,30)

console.log(res1)
console.log(res2)

Open a terminal and run the following command:

node –experimental-modules main.mjs

Output:

75
48

Summary

Some solutions have been given in this article to help you fix the error unexpected token import syntaxerror in node.js. Apply it to your project and get the result.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *