close

[Solved] Node.js – SyntaxError: Unexpected token import

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Node.js – SyntaxError: Unexpected token import in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Node.js – SyntaxError: Unexpected token import Error Occurs?

Today I get the following error Node.js – SyntaxError: Unexpected token import in javascript.

How To Solve Node.js – SyntaxError: Unexpected token import Error ?

  1. How To Solve Node.js – SyntaxError: Unexpected token import Error ?

    To Solve Node.js – SyntaxError: Unexpected token import Error In case that you still can't use “import” here is how I handled it: Just translate it to a node friendly require. Example:
    import { parse } from 'node-html-parser';

  2. Node.js – SyntaxError: Unexpected token import

    To Solve Node.js – SyntaxError: Unexpected token import Error In case that you still can't use “import” here is how I handled it: Just translate it to a node friendly require. Example:
    import { parse } from 'node-html-parser';

Solution 1

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

Solution 2

In case that you still can’t use “import” here is how I handled it: Just translate it to a node friendly require. Example:

import { parse } from 'node-html-parser';

Is the same as:

const parse = require('node-html-parser').parse;

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment