How can I fix NPM which always says unexpected token = - npm

After my machine updated automatically, NPM is not working any more
OS: Ubuntu 20.04.3 LTS
npm -v: 8.1.4
node -v: v10.19.0
Every time I try to start something with NPM, it gives me the same error message:
*#*:~$ npm help
npm WARN npm npm does not support Node.js v10.19.0
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm You can find the latest version at https://nodejs.org/
npm ERR! Unexpected token =
npm ERR! A complete log of this run can be found in:
npm ERR! /home/*/.npm/_logs/2021-11-19T13_44_25_087Z-debug.log
The Error log contains the following:
0 verbose stack /usr/local/lib/node_modules/npm/lib/commands/help.js:16
30 verbose stack static description = 'Get help on npm'
30 verbose stack ^
30 verbose stack
30 verbose stack SyntaxError: Unexpected token =
30 verbose stack at Module._compile (internal/modules/cjs/loader.js:723:23)
30 verbose stack at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
30 verbose stack at Module.load (internal/modules/cjs/loader.js:653:32)
30 verbose stack at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
30 verbose stack at Function.Module._load (internal/modules/cjs/loader.js:585:3)
30 verbose stack at Module.require (internal/modules/cjs/loader.js:692:17)
30 verbose stack at require (internal/modules/cjs/helpers.js:25:18)
30 verbose stack at Npm.cmd (/usr/local/lib/node_modules/npm/lib/npm.js:97:18)
31 verbose cwd /home/*
32 verbose Linux 5.11.0-40-generic
33 verbose argv "/usr/bin/node" "/usr/local/bin/npm" "help"
34 verbose node v10.19.0
35 verbose npm v8.1.4
36 error Unexpected token =
37 verbose exit 1

I solved it like this:
Install nvm
sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.profile
nvm install node
Source

Upgrade your node version, you're using the newest npm version, and it doesn't support old versions of node
Since you're on ubuntu i recommend you to use nvm
sudo apt-get install nvm
nvm install 16
nvm use 16

npm is broken because it will ignore PATH, and it will ignore the calling runtime, instead it will use an arbitrary runtime.
sudo which node
/usr/local/bin/node
.
sudo /usr/local/bin/node --version
v17.1.0
.
sudo /usr/local/bin/node /usr/local/bin/npm install -g node#latest
...
npm ERR! npm WARN npm npm does not support Node.js v10.19.0
...
removing the distribution version might be a workaround.

Related

npm 8.5.5: unexected token in #npmcli\aborist

after upgrading npm (via nvm) to 8.5.5 npm commands do not work anymore:
e.g. npm --verbose ls webpack fails with:
npm verb stack C:\Users\marti\AppData\Roaming\nvm\v16.15.0\node_modules\npm\node_modules\#npmcli\arborist:1
npm verb stack ../../workspaces/arborist
npm verb stack ^
npm verb stack
npm verb stack SyntaxError: Unexpected token '.'
npm verb stack at Object.compileFunction (node:vm:352:18)
npm verb stack at wrapSafe (node:internal/modules/cjs/loader:1033:15)
npm verb stack at Module._compile (node:internal/modules/cjs/loader:1069:27)
npm verb stack at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
npm verb stack at Module.load (node:internal/modules/cjs/loader:981:32)
npm verb stack at Function.Module._load (node:internal/modules/cjs/loader:822:12)
npm verb stack at Module.require (node:internal/modules/cjs/loader:1005:19)
npm verb stack at require (node:internal/modules/cjs/helpers:102:18)
npm verb stack at Object.<anonymous> (C:\Users\marti\AppData\Roaming\nvm\v16.15.0\node_modules\npm\lib\commands\ls.js:7:18)
npm verb stack at Module._compile (node:internal/modules/cjs/loader:1105:14)
npm verb cwd D:\dev\xxx
npm verb Windows_NT 10.0.19043
npm verb argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "--verbose" "ls" "webpack"
npm verb node v16.15.0
npm verb npm v8.5.5
npm ERR! Unexpected token '.'
Note: when I use nvm and switch back to node 16.12 (npm version 8.1.0) it works fine
Details
The file that the error refers to
C:\Users\xxx\AppData\Roaming\nvm\v16.15.0\node_modules\npm\node_modules\#npmcli\arborist contains only this (which is not valid js I guess):
../../workspaces/arborist
What I have tried (and did not work)
restarting my PC
uninstalling node 16.15 via nvm and reinstalling it
Workaround
delete the file C:\Users\xxx\AppData\Roaming\nvm\v16.15.0\node_modules\npm\node_modules\#npmcli\arborist
then copy the aborist directory from an older node version to that directory: e.g. from C:\Users\xxx\AppData\Roaming\nvm\v16.12.0\node_modules\npm\node_modules\#npmcli\arborist\
Any ideas what I am doing wrong? Or is this just a bug in npm 8.5.5
This worked for me:
updated nvm to version 1.1.9 (old installed version was 1.1.7)
then reinstalled node version 16.15.0 (now aborist is a directory) and the commands work as expected

How to give mkdir permission for npx command

I have problem using npx create-react-app command
Earlier I had create-react-app installed globally on and system and used npm create-react-app command
But I learned that using npx we will always use the latest version and to use npx I have uninstalled create-react-app from my system
Whenever I try to run npx create-react-app my-app command
I get the following error:
C:\Users\Sachin Verma\Desktop\react-project>npx create-react-app tut
Error: EPERM: operation not permitted, mkdir 'C:\Users\Sachin'
command not found: create-react-app
But if I install create-react-app on my system globally, then npx command works
I tried cleaning npm cache and also reinstalled node.js but the problem still exists
Edit:
When running CMD in administrator rights
Microsoft Windows [Version 10.0.19042.685]
(c) 2020 Microsoft Corporation. All rights reserved.
C:\WINDOWS\system32>cd C:\Users\Sachin Verma\Desktop\react-project\
C:\Users\Sachin Verma\Desktop\react-project>npx create-react-app tutt
npm ERR! code ENOLOCAL
npm ERR! Could not install from "Verma\AppData\Roaming\npm-cache\_npx\14832" as it does not contain a package.json file.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Sachin Verma\AppData\Roaming\npm-cache\_logs\2021-01-11T15_27_28_701Z-debug.log
Install for [ 'create-react-app#latest' ] failed with code 1
Content of log file:
0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli 'C:\\Users\\Sachin Verma\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli 'install',
1 verbose cli 'create-react-app#latest',
1 verbose cli '--global',
1 verbose cli '--prefix',
1 verbose cli 'C:\\Users\\Sachin',
1 verbose cli 'Verma\\AppData\\Roaming\\npm-cache\\_npx\\14832',
1 verbose cli '--loglevel',
1 verbose cli 'error',
1 verbose cli '--json'
1 verbose cli ]
2 info using npm#6.14.11
3 info using node#v15.5.1
4 verbose npm-session 5f8e13bb496752c5
5 silly install loadCurrentTree
6 silly install readGlobalPackageData
7 silly fetchPackageMetaData error for file:Verma\AppData\Roaming\npm-cache\_npx\14832 Could not install from "Verma\AppData\Roaming\npm-cache\_npx\14832" as it does not contain a package.json file.
8 http fetch GET 304 https://registry.npmjs.org/create-react-app 2907ms (from cache)
9 silly pacote tag manifest for create-react-app#latest fetched in 2943ms
10 timing stage:rollbackFailedOptional Completed in 2ms
11 timing stage:runTopLevelLifecycles Completed in 2957ms
12 verbose stack Error: ENOENT: no such file or directory, open 'C:\Users\Sachin Verma\Desktop\react-project\Verma\AppData\Roaming\npm-cache\_npx\14832\package.json'
13 verbose cwd C:\Users\Sachin Verma\Desktop\react-project
14 verbose Windows_NT 10.0.19042
15 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Sachin Verma\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "install" "create-react-app#latest" "--global" "--prefix" "C:\\Users\\Sachin" "Verma\\AppData\\Roaming\\npm-cache\\_npx\\14832" "--loglevel" "error" "--json"
16 verbose node v15.5.1
17 verbose npm v6.14.11
18 error code ENOLOCAL
19 error Could not install from "Verma\AppData\Roaming\npm-cache\_npx\14832" as it does not contain a package.json file.
20 verbose exit [ 1, true ]
Is there any problem due to my username because it contains a space in between?
Try to run your CMD in administration or you can change your working dir, where no admin privilege is required.

Socket timeout when trying to create a new vue project with vue-cli

I'm getting an error when I create a new Vue project using vue-cli. Each time the create fails on a "Socket timeout" but a different request? I updated node using nvm and installed the latest vue-cli.
Terminal shows this:
Vue CLI v4.5.9
✨ Creating project in /Users/me/Documents/GitHub/myproject.
🗃 Initializing git repository...
⚙️ Installing CLI plugins. This might take a while...
npm ERR! code ERR_SOCKET_TIMEOUT
npm ERR! errno ERR_SOCKET_TIMEOUT
npm ERR! request to https://registry.npm.taobao.org/cosmiconfig failed, reason: Socket timeout
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/me/.npm/_logs/2020-12-07T11_02_37_391Z-debug.log
ERROR command failed: npm install --loglevel error --legacy-peer-deps
The create tends to fail around the log file is around line 4900. These are the last lines in the log file:
4875 timing idealTree:node_modules/webpack-dev-server/node_modules/anymatch/node_modules/normalize-path Completed in 0ms
4876 timing idealTree:buildDeps Completed in 164233ms
4877 timing idealTree:fixDepFlags Completed in 10ms
4878 timing idealTree Completed in 164265ms
4879 timing command:install Completed in 164269ms
4880 verbose type system
4881 verbose stack FetchError: request to https://registry.npm.taobao.org/cosmiconfig failed, reason: Socket timeout
4881 verbose stack at ClientRequest.<anonymous> (/Users/me/.nvm/versions/node/v15.3.0/lib/node_modules/npm/node_modules/minipass-fetch/lib/index.js:97:14)
4881 verbose stack at ClientRequest.emit (node:events:376:20)
4881 verbose stack at TLSSocket.socketErrorListener (node:_http_client:490:9)
4881 verbose stack at TLSSocket.emit (node:events:388:22)
4881 verbose stack at emitErrorNT (node:internal/streams/destroy:188:8)
4881 verbose stack at emitErrorCloseNT (node:internal/streams/destroy:153:3)
4881 verbose stack at processTicksAndRejections (node:internal/process/task_queues:80:21)
4882 verbose cwd /Users/me/Documents/GitHub/myproject
4883 verbose Darwin 19.6.0
4884 verbose argv "/Users/me/.nvm/versions/node/v15.3.0/bin/node" "/Users/me/.nvm/versions/node/v15.3.0/bin/npm" "install" "--loglevel" "error" "--legacy-peer-deps"
4885 verbose node v15.3.0
4886 verbose npm v7.0.14
4887 error code ERR_SOCKET_TIMEOUT
4888 error errno ERR_SOCKET_TIMEOUT
4889 error request to https://registry.npm.taobao.org/cosmiconfig failed, reason: Socket timeout
4890 verbose exit 1
I ran into this as well. Fixed it by first running
vue config -e
Then setting this property to false:
{
"useTaobaoRegistry": false
}
Ok, I just updated my os to Big Sur and now it seems to work.
Updating to Big Sur did not help me.
This, however, helped:
sudo npm uninstall -g vue
sudo npm uninstall -g vue-cli
sudo npm uninstall -g #vue/cli
sudo npm cache clean --force
sudo npm install -g vue
sudo npm install -g #vue/cli

npm install axios has ssl3_get_record error

there, I currently want to use npm install vue and axios. My computer was behind proxy, I also set the proxy. but still can't work.
Here is the work i have done:
npm install axios
and then i get this kind of error
[yliu#rhlab1 ~]$ npm install axios
npm ERR! code EPROTO
npm ERR! errno EPROTO
npm ERR! request to http://registry.npmjs.org/axios failed, reason: write EPROTO 139889298220864:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:ssl/record/ssl3_record.c:332:
npm ERR!
npm ERR! A complete log of this run can be found in:
npm ERR! /home/yliu/.npm/_logs/2020-03-16T18_27_02_693Z-debug.log
and the log it show like this :
[yliu#rhlab1 ~]$ cat /home/yliu/.npm/_logs/2020-03-16T18_27_02_693Z-debug.log | nl | tail -n 10
24 12 verbose stack at WriteWrap.afterWrite [as oncomplete] (net.js:790:10)
25 13 verbose cwd /home/yliu
26 14 verbose Linux 4.18.0-80.1.2.el8_0.x86_64
27 15 verbose argv "/usr/bin/node" "/usr/bin/npm" "install" "axios"
28 16 verbose node v10.14.1
29 17 verbose npm v6.4.1
30 18 error code EPROTO
31 19 error errno EPROTO
32 20 error request to http://registry.npmjs.org/axios failed, reason: write EPROTO 139889298220864:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:ssl/record/ssl3_record.c:332:
33 21 verbose exit [ 1, true ]
set the proxy (for privacy didn't put my real proxy server):
npm config set http-proxy http://proxy.example.com:8080
npm config set https-proxy https://proxy.example.com:8080
and show it
[yliu#rhlab1 ~]$ npm config list
; cli configs
metrics-registry = "http://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.4.1 node/v10.14.1 linux x64"
; userconfig /home/yliu/.npmrc
http-proxy = "http://proxy.example.com:8080"
https-proxy = "https://proxy.example.com:8080/"
registry = "http://registry.npmjs.org/"
; node bin location = /usr/bin/node
; cwd = /home/yliu
; HOME = /home/yliu
; "npm config ls -l" to show all defaults.
I appropriate if anyone can give me some advices.
Thanks in advance!

Issue Installing Polymer on Arch Linux via npm

I get the following error when installing Polymer CLI as follows
sudo npm install -g polymer-cli
29577 info lifecycle wd#1.5.0~install: wd#1.5.0
29578 verbose lifecycle wd#1.5.0~install: unsafe-perm in lifecycle false
29579 verbose lifecycle wd#1.5.0~install: PATH: /usr/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/usr/lib/node_modules/polymer-cli/node_modules/wd/node_modules/.bin:/usr/lib/node_modules/polymer-cli/node_modules/.bin:/usr/lib/node_modules/.bin:/usr/local/sbin:/usr/loc\
al/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
29580 verbose lifecycle wd#1.5.0~install: CWD: /usr/lib/node_modules/polymer-cli/node_modules/wd
29581 silly lifecycle wd#1.5.0~install: Args: [ '-c', 'node scripts/build-browser-scripts' ]
29582 silly lifecycle wd#1.5.0~install: Returned: code: 1 signal: null
29583 info lifecycle wd#1.5.0~install: Failed to exec install script
29584 verbose unlock done using /root/.npm/_locks/staging-a072192f34a17023.lock for /usr/lib/node_modules/.staging
29585 verbose stack Error: wd#1.5.0 install: `node scripts/build-browser-scripts`
29585 verbose stack Exit status 1
29585 verbose stack at EventEmitter.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:285:16)
29585 verbose stack at EventEmitter.emit (events.js:160:13)
29585 verbose stack at ChildProcess.<anonymous> (/usr/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
29585 verbose stack at ChildProcess.emit (events.js:160:13)
29585 verbose stack at maybeClose (internal/child_process.js:943:16)
29585 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:220:5)
29586 verbose pkgid wd#1.5.0
Any idea how to fix it?
UPDATE
I updated the npm and bower to the latest versions and then ran the install command again for polymer-cli. Now I get the following error message - it's a permissioning problem when running the script. I am not sure if this is down to the npm install script or if it is an arch linux problem.
sudo npm install -g polymer-cli
npm WARN deprecated babel-preset-es2015#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
npm WARN deprecated bower#1.8.2: ...psst! Your project can stop working at any moment because its dependencies can change. Prevent this by migrating to Yarn: https://bower.io/blog/2017/how-to-migrate-away-from-bower/
npm WARN deprecated github#7.3.2: 'github' has been renamed to '#octokit/rest' (https://git.io/vNB11)
npm WARN deprecated #types/assert#0.0.29: See https://github.com/DefinitelyTyped/DefinitelyTyped/issues/12826
/usr/bin/polymer -> /usr/lib/node_modules/polymer-cli/bin/polymer.js
> uws#0.14.5 install /usr/lib/node_modules/polymer-cli/node_modules/uws
> node-gyp rebuild > build_log.txt 2>&1 || exit 0
sh: build_log.txt: Permission denied
> wd#1.5.0 install /usr/lib/node_modules/polymer-cli/node_modules/wd
> node scripts/build-browser-scripts
/usr/lib/node_modules/polymer-cli/node_modules/mkdirp/index.js:90
throw err0;
^
Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/polymer-cli/node_modules/wd/build'
at Object.fs.mkdirSync (fs.js:905:18)
at sync (/usr/lib/node_modules/polymer-cli/node_modules/mkdirp/index.js:71:13)
at Object.<anonymous> (/usr/lib/node_modules/polymer-cli/node_modules/wd/scripts/build-browser-scripts.js:6:1)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
at startup (bootstrap_node.js:193:16)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! wd#1.5.0 install: `node scripts/build-browser-scripts`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the wd#1.5.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2018-01-28T19_51_38_197Z-debug.log
The issue now is when the script tries to make this directory:
mkdir '/usr/lib/node_modules/polymer-cli/node_modules/wd/build'
use --unsafe-perm option to install polymer-cli globally as described here
sudo npm install -g polymer-cli --unsafe-perm
or Change npm's Default Directory
and then install npm install -g polymer-cli without sudo and --unsafe-perm option.
Switched to Yarn and it installs!
sudo yarn global add polymer-cli
yarn global v1.3.2
[1/4] Resolving packages...
warning polymer-cli > babel-preset-es2015#6.24.1: 🙌 Thanks for using Babel: we recommend using babel-preset-env now: please read babeljs.io/env to update!
warning polymer-cli > bower#1.8.2: ...psst! Your project can stop working at any moment because its dependencies can change. Prevent this by migrating to Yarn: https://bower.io/blog/2017/how-to-migrate-away-from-bower/
warning polymer-cli > github#7.3.2: 'github' has been renamed to '#octokit/rest' (https://git.io/vNB11)
warning polymer-cli > polyserve > #types/assert#0.0.29: See https://github.com/DefinitelyTyped/DefinitelyTyped/issues/12826
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "polymer-cli#1.5.7" with binaries:
- polymer
Done in 61.35s.