9

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
    └─┬ resclient@2.0.2
    
  • 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.

Arran Duff
  • 1,214
  • 2
  • 11
  • 23
  • 2
    Possible duplicate of [How can I use an es6 import in node?](https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node) – Seblor Mar 05 '19 at 12:25
  • @Seblor I disagree. I have seen that post. imports only work natively with node.js after version 8.50. I have upgraded my version to 11.1, so that is no longer an issue for me. If you look at the error they are talking about it is different – Arran Duff Mar 05 '19 at 12:27
  • 1
    @ArranDuff I'm afraid you're only partially correct, it is supported in Node 8.5+ but only with the `--experimental-modules` flag enabled (which the answer linked does explain). So your options are to either enable that flag or use a transpiler like Babel. – James Mar 05 '19 at 12:30
  • Ok, fair enough looks like I misread the post. Thanks guys – Arran Duff Mar 05 '19 at 12:49

2 Answers2

5

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

Aldo Porcallo
  • 142
  • 1
  • 4
0

I was also facing the same issue. Here is how I fixed it.

I first updated my node and its package manager version using the following commands :

npm install node@latest -g

and

npm install npm@latest -g

After doing so, I went to my package.json file and added the following line before the scripts property:

"type": "module",

And last, I went back and tried to run my code using node app.js but it did not work until I used the following command:

node --experimental-modules app.js

At your side, replace app.js with your index file. Hope it also works for you

Nice coding

matthias_h
  • 11,356
  • 9
  • 22
  • 40
Desire Kaleba
  • 1,264
  • 11
  • 11