Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing node.js module - SyntaxError: Unexpected identifier

Tags:

node.js

I'm trying to do the following import

import  ResClient from 'resclient';

Result

/home/arran/WebstormProjects/untitled1/app.js:2
import  ResClient from 'resclient';
        ^^^^^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:85:7)
    at createScript (vm.js:266:10)
    at Object.runInThisContext (vm.js:314:10)
    at Module._compile (internal/modules/cjs/loader.js:698:28)
    at Object.Module._extensions..js 
(internal/modules/cjs/loader.js:749:10)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:801:12)
  • The module documentation is located here and does the exact same import in the example.
  • I have installed the module globally.

    npm list -g | grep resclient
    └─┬ [email protected]
    
  • I am using the following node version

     $node -v
     v11.10.1
    

I have seen some other posts about this error. For example here but it is a browser based problem using client side javascript rather than node. I'm a node newbie, so would appreciate any pointers no matter how basic.

like image 619
Arran Duff Avatar asked Mar 05 '19 12:03

Arran Duff


People also ask

How do you fix an unexpected identifier?

To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.

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 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.

How do I check node js version?

To see if Node is installed, open the Windows Command Prompt, Powershell or a similar command line tool, and type node -v . This should print the version number so you'll see something like this v0. 10.35 .


1 Answers

As for node 12, you can add the property type in your package.json as

"type": "module"

and dont forget to run node with experimental-modules flag node --experimental-modules youapp.js

like image 165
Aldo Porcallo Avatar answered Sep 23 '22 18:09

Aldo Porcallo