'unknown error on npm install "npm ERR! Unexpected token '.'" on using NVM-Windows, Node v17, NPM v8

This seems to be a semi-common error, but it's unclear to me what is causing it. I want to know what is causing this error and how can I resolve it.

Using NVM I have installed Node @latest (Version 17.4.0), which has installed npm's matching latest (v8.3.1).

PS C:\myRepo> node -v
v17.4.0
PS C:\myRepo> npm -v
8.3.1
PS C:\myRepo> nvm list

  * 17.4.0 (Currently using 64-bit executable)
    17.0.0
    15.6.0
    14.18.3
    14.15.4
    12.20.1

I then go to install a package dependency from npm and it gives me an error regardless of the package name:

PS C:\myRepo> npm install -g npm
npm ERR! Unexpected token '.'

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\yodama\AppData\Local\npm-cache\_logs\2022-01-25T16_38_44_607Z-debug-0.log

And we can see the full debug log:

0 verbose cli [
0 verbose cli   'C:\\Program Files\\nodejs\\node.exe',
0 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js',
0 verbose cli   'install',
0 verbose cli   '-g',
0 verbose cli   'npm'
0 verbose cli ]
1 info using [email protected]
2 info using [email protected]
3 timing npm:load:whichnode Completed in 0ms
4 timing config:load:defaults Completed in 1ms
5 timing config:load:file:C:\Users\yodama\AppData\Roaming\nvm\v17.4.0\node_modules\npm\npmrc Completed in 3ms
6 timing config:load:builtin Completed in 3ms
7 timing config:load:cli Completed in 3ms
8 timing config:load:env Completed in 0ms
9 timing config:load:project Completed in 3ms
10 timing config:load:file:C:\Users\yodama\.npmrc Completed in 4ms
11 timing config:load:user Completed in 4ms
12 timing config:load:file:C:\Program Files\nodejs\etc\npmrc Completed in 0ms
13 timing config:load:global Completed in 0ms
14 timing config:load:validate Completed in 0ms
15 timing config:load:credentials Completed in 1ms
16 timing config:load:setEnvs Completed in 1ms
17 timing config:load Completed in 19ms
18 timing npm:load:configload Completed in 20ms
19 timing npm:load:setTitle Completed in 1ms
20 timing config:load:flatten Completed in 4ms
21 timing npm:load:display Completed in 7ms
22 verbose logfile C:\Users\yodama\AppData\Local\npm-cache\_logs\2022-01-25T16_38_44_607Z-debug-0.log
23 timing npm:load:logFile Completed in 13ms
24 timing npm:load:timers Completed in 0ms
25 timing npm:load:configScope Completed in 0ms
26 timing npm:load Completed in 42ms
27 verbose stack C:\Users\yodama\AppData\Roaming\nvm\v17.4.0\node_modules\npm\node_modules\libnpmfund:1
27 verbose stack ../workspaces/libnpmfund
27 verbose stack ^
27 verbose stack
27 verbose stack SyntaxError: Unexpected token '.'
27 verbose stack     at Object.compileFunction (node:vm:352:18)
27 verbose stack     at wrapSafe (node:internal/modules/cjs/loader:1026:15)
27 verbose stack     at Module._compile (node:internal/modules/cjs/loader:1061:27)
27 verbose stack     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
27 verbose stack     at Module.load (node:internal/modules/cjs/loader:975:32)
27 verbose stack     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
27 verbose stack     at Module.require (node:internal/modules/cjs/loader:999:19)
27 verbose stack     at require (node:internal/modules/cjs/helpers:102:18)
27 verbose stack     at Object.<anonymous> (C:\Users\yodama\AppData\Roaming\nvm\v17.4.0\node_modules\npm\lib\utils\reify-output.js:16:38)
27 verbose stack     at Module._compile (node:internal/modules/cjs/loader:1097:14)
28 verbose cwd C:\myRepo
29 verbose Windows_NT 10.0.22000
30 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "-g" "npm"
31 verbose node v17.4.0
32 verbose npm  v8.3.1
33 error Unexpected token '.'
34 verbose exit 1
35 timing npm Completed in 262ms
36 verbose code 1

switching using nvm for windows to Node 14 fixes this issue:


PS C:\myRepo> nvm use 14.18.3
Now using node v14.18.3 (64-bit)
PS C:\myRepo> node -v 
v14.18.3
PS C:\myRepo> npm -v
6.14.15
PS C:\myRepo> npm i -g @microsoft/rush
npm WARN deprecated @opentelemetry/[email protected]: Package renamed to @opentelemetry/api, see https://github.com/open-telemetry/opentelemetry-js
C:\Program Files\nodejs\rush -> C:\Program Files\nodejs\node_modules\@microsoft\rush\bin\rush
C:\Program Files\nodejs\rushx -> C:\Program Files\nodejs\node_modules\@microsoft\rush\bin\rushx
+ @microsoft/[email protected]
added 286 packages from 296 contributors in 26.69s

What is going wrong here? Is this an NVM issue, or a npm 8.x issue? I need to do testing in Node v17 for a specific bug, so just using the older versions of Node isn't a viable workaround.



Solution 1:[1]

Update the NVM-Windows to the newest version, which is by now 1.1.9

They've had fixed this issue, Workspace folder must be symlink.

The issue goes with NVM-Windows not NPM

Solution 2:[2]

use a lower version of node.

nvm list -> to see the installed node versions.

nvm use <-version number->

run npm install again.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JARVISAI
Solution 2 jo3rn