Why npm -v is slow? - node.js

Why does showing npm version command takes more than 1 second? what does it do under the hood?
The command I run on my MacOS:
$ npm -v
8.15.0
Version: Monterey 12.6.1 (21G217)
Updated Feb 15
npm is a symlink to npm-cli.js and npm-cli.js only contains 2 lines of code. So the question is:
Why the 2 lines of code is so slow?
$ cat /usr/local/lib/node_modules/npm/bin/npm-cli.js
#!/usr/bin/env node
require('../lib/cli.js')(process)
$ which npm
/usr/local/bin/npm
$ll /usr/local/bin/npm
lrwxr-xr-x 1 root wheel 38 Sep 28 12:03 /usr/local/bin/npm -> ../lib/node_modules/npm/bin/npm-cli.js

Related

npm ERR! code EACCES, npm install --global yarn met error

Mac mini (M1, 2020)
Monterey
Brownie v1.17.2
nodejs v16.13.4
I am learning solidity according to reference(https://www.youtube.com/watch?v=M576WGiDBdQ&t=25510s).
Node.JS install is fine
when I tried this
npm install --global yarn
the terminal give the error information
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/yarn
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'
npm ERR! [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/yarn'] {
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'mkdir',
npm ERR! path: '/usr/local/lib/node_modules/yarn'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/liwei/.npm/_logs/2022-01-08T12_13_37_765Z-debug.log
(base) liwei#liweideMac-mini-2 com~apple~CloudDocs %
I checked the owner of the file ,the owner is root ,should i use sudo?
(base) liwei#liweideMac-mini-2 com~apple~CloudDocs % ls -l /usr/local/lib/node_modules/npm
total 64
-rw-r--r-- 1 root wheel 9742 10 14 08:49 LICENSE
-rw-r--r-- 1 root wheel 4190 10 19 06:36 README.md
drwxr-xr-x 9 root wheel 288 12 1 19:46 bin
drwxr-xr-x 4 root wheel 128 12 1 19:46 docs
-rw-r--r-- 1 root wheel 145 11 23 14:00 index.js
drwxr-xr-x 77 root wheel 2464 12 1 19:46 lib
drwxr-xr-x 5 root wheel 160 12 1 19:46 man
drwxr-xr-x 188 root wheel 6016 12 1 19:46 node_modules
-rw-r--r-- 1 root wheel 6007 12 1 19:13 package.json
drwxr-xr-x 3 root wheel 96 12 1 19:46 tap-snapshots
(base) liwei#liweideMac-mini-2 com~apple~CloudDocs %
find this thread (Error: EACCES: permission denied, access '/usr/local/lib/node_modules'),changed the owner , stll one file don't change.
(base) liwei#liweideMac-mini-2 com~apple~CloudDocs % sudo chown -R liwei: /usr/local/lib/node_modules
(base) liwei#liweideMac-mini-2 com~apple~CloudDocs % ls -la /usr/local/lib/node_modules
total 0
drwxr-xr-x 4 liwei wheel 128 1 8 11:51 .
drwxr-xr-x 4 root wheel 128 1 8 11:51 ..
drwxr-xr-x 7 liwei wheel 224 12 1 19:46 corepack
drwxr-xr-x 13 liwei wheel 416 12 1 19:46 npm
what is this 2 dots is?
reference answer is here
(Error: EACCES: permission denied, access '/usr/local/lib/node_modules')
This works for me very quick.
To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use a hidden directory in your home directory.
Back up your computer. On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create a
~/.profile
file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
To test your new configuration, install a package globally without using sudo
I had the same problem but I could solve it after seeing a youtube video
use this command:
sudo npm install --global yarn

"npm -g yarn" gives "permission denied, symlink..." but the proper permissions do exists [duplicate]

This question already has answers here:
NPM modules won't install globally without sudo
(16 answers)
Closed 2 years ago.
I need to install yarn without sudo. I do this:
$ npm install -g yarn
and I get this:
npm ERR! code EACCES
npm ERR! syscall symlink
npm ERR! path ../lib/node_modules/yarn/bin/yarn.js
npm ERR! dest /usr/bin/yarn
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/yarn/bin/yarn.js' -> '/usr/bin/yarn'
npm ERR! [OperationalError: EACCES: permission denied, symlink '../lib/node_modules/yarn/bin/yarn.js' -> '/usr/bin/yarn'] {
npm ERR! cause: [Error: EACCES: permission denied, symlink '../lib/node_modules/yarn/bin/yarn.js' -> '/usr/bin/yarn'] {
[.........]
But:
ls -al /usr/lib/node_modules/
drwxr-xr-x 5 user1 user1 4096 Sep 15 05:13 .
drwxr-xr-x 63 root root 32768 Sep 14 15:18 ..
drwxr-xr-x 9 user1 user1 4096 Sep 14 14:48 node-gyp
drwxr-xr-x 9 user1 user1 4096 Sep 15 04:22 npm
drwxr-xr-x 7 user1 user1 4096 Sep 14 14:48 semver
And
$ ls -ald /usr/lib/node_modules/
drwxr-xr-x 5 user1 user1 4096 Sep 15 05:13 /usr/lib/node_modules/
Why and how to fix it?
Your config folder must be owned by root, which creates several permissions issue. Changing it to the current user will solve it
$USER:$GROUP -> gives the current login user and the group. in the terminal you can try to echo $USER echo $GROUP to know its value. By this command, it will change to the value present in $USER. The same applies for the group too.
sudo chown -R $USER:$GROUP ~/.npm
sudo chown -R $USER:$GROUP ~/.config
now try all your commands
replication of https://github.com/bower/bower/issues/2262

Unable to install Gauge with npm

I've followed the instructions verbatim to install gauge from "https://gauge.org/getting-started-guide/quick-install/" and I'm unable to install it.
I'm starting from a fresh GCP Debian instance. After installing the NodeJS PPM and then following the instructions, "gauge" is installing a blank file under /usr/lib/node_modules/#getgauge/cli/bin. I'm getting the following file permission error. This is all run as root.
root#instance-1:~# npm -v
6.9.0
root#instance-1:~# node -v
v10.16.3
root#instance-1:~# npm install -g npm#latest
/usr/bin/npm -> /usr/lib/node_modules/npm/bin/npm-cli.js
/usr/bin/npx -> /usr/lib/node_modules/npm/bin/npx-cli.js
+ npm#6.11.2
added 19 packages from 13 contributors, removed 15 packages and updated 53 packages in 6.895s
root#instance-1:~# npm install -g #getgauge/cli
/usr/bin/gauge -> /usr/lib/node_modules/#getgauge/cli/bin/gauge
> #getgauge/cli#1.0.5 install /usr/lib/node_modules/#getgauge/cli
> node ./src/index.js
Fetching download url for Gauge version 1.0.5
Downloading https://github.com/getgauge/gauge/releases/download/v1.0.5/gauge-1.0.5-linux.x86_64.zip to ./bin
{ [Error: EACCES: permission denied, open 'bin/gauge'] errno: -13, code: 'EACCES', syscall: 'open', path: 'bin/gauge' }
+ #getgauge/cli#1.0.5
added 83 packages from 76 contributors in 6.48s
Version information
root#gauge:~/gaugetest$ npm -v
6.11.2
root#gauge:~/gaugetest$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 9.9 (stretch)
Release: 9.9
Codename: stretch
Any ideas?
npm changes the uid to the user account or uid specified by the user config when invoked with root privileges, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.
In your case you can try npm run #getgauge/cli --uynsafe-perm

Error with 'npm start'

I'm using Node v4.2.6 and npm v3.5.2 on Ubuntu, I clone my proyect from github and install all the modules. When I do npm start its run fine, but when I try to run it again start crashing and the only way I find to make it work again is to re-clone the project.
It's a rare and dummy question i think, can someone help me? Pardon my bad english, here is the npm-debug.log error:
17 error Linux 4.4.0-92-generic
18 error argv "/usr/bin/nodejs" "/usr/bin/npm" "run" "lite"
19 error node v4.2.6
20 error npm v3.5.2
21 error code ELIFECYCLE
22 error firstApp-client#1.0.0 lite: lite-server
22 error Exit status 1
23 error Failed at the firstApp-client#1.0.0 lite script 'lite-server'.
23 error Make sure you have the latest version of node.js and npm installed.
23 error If you do, this is most likely a problem with the firstApp-client package,
23 error not with npm itself.
23 error Tell the author that this fails on your system:
23 error lite-server
23 error You can get information on how to open an issue for this project with:
23 error npm bugs firstApp-client
23 error Or if that isn't available, you can get their info via:
23 error npm owner ls firstApp-client
23 error There is likely additional logging output above.
24 verbose exit [ 1, true ]
I don't know why but it seems to be that the problem was an incompatibility of the versions of nodejs.
I run:
$ sudo npm cache clean -f
$ sudo npm install -g n
$ sudo n stable
$ sudo ln -sf /usr/local/n/versions/node/<VERSION>/bin/node /usr/bin/nodejs
And all was resolve. Thanks for the comments

Equivalent builds don't behave the same

if I build an image like this:
docker build -f app/Dockerfile -t test .
it builds just fine, but the following docker-compose.yml (which I believe should be equivalent):
app:
container_name: test
build:
context: .
dockerfile: app/Dockerfile
fails to build. if I run docker-compose with --verbose I see:
compose.cli.verbose_proxy.proxy_callable: docker build <- (pull=False,
stream=True, nocache=False, tag=u'prj_app', buildargs=None,
rm=True, forcerm=False, path='/Users/ekkis/dev/prj',
dockerfile='./app/Dockerfile')
which would seem pretty much what I expect. specifically the error I'm getting is this (sorry to include as code but quotes make a mess of things):
npm WARN app#1.0.0 No description
npm WARN app#1.0.0 No repository field.
npm ERR! Linux 4.9.12-moby
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "/tmp/inc"
npm ERR! node v7.7.3
npm ERR! npm v4.1.2
npm ERR! path /usr/src/app/node_modules/inc
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall access
npm ERR! enoent ENOENT: no such file or directory, access '/usr/src/app/node_modules/inc'
npm ERR! enoent ENOENT: no such file or directory, access '/usr/src/app/node_modules/inc'
npm ERR! enoent This is most likely not a problem with npm itself
npm ERR! enoent and is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! Please include the following file with any support request:
npm ERR! /usr/src/app/npm-debug.log
as you can see from the above, the Dockerfile performs an npm install /tmp/inc, which from the output I see succeeded since it prints a tree of modules installed. /tmp/inc is created by the Dockerfile command shown below as successful:
Step 8/11 : ADD inc /tmp/inc
---> Using cache
---> fce607bb13ca
and I note that the app/npm-debug.log was not written to, nor is there a log file in the directory where I ran docker compose.
what could be the issue here? why does it build ok manually but not with docker-compose?
* Addendum I *
in my continuing efforts to figure it out, I added a directory listing before the Dockerfile executes the npm install...
Step 9/13 : RUN ls -alF
---> Running in ac0c62b7da93
total 28
drwxr-xr-x 3 root root 4096 Mar 19 07:01 ./
drwxr-xr-x 4 root root 4096 Mar 19 07:01 ../
-rw-r--r-- 1 root root 722 Mar 19 05:48 config.json
-rw-r--r-- 1 root root 3786 Mar 19 05:48 index.js
drwxr-xr-x 119 root root 4096 Mar 17 18:57 node_modules/
-rw-r--r-- 1 root root 2910 Mar 17 20:51 npm-debug.log
-rw-r--r-- 1 root root 416 Mar 19 06:39 package.json
...and, most interestingly, there is already a node_modules directory there!! that means the problem is that docker-compose is not honouring the .dockerignore file (which prevents copying **/node_modules into the context)
so is there some special voodoo I have to perform for the build to generate the context properly?
* Addendum II *
here's my Dockerfile:
FROM node:latest
EXPOSE 8000
ENV PORT 8000
RUN apt-get update
RUN groupadd -r tst && useradd -m -r -g tst tst
WORKDIR /usr/src/app
ADD app /usr/src/app
ADD inc /tmp/inc
RUN npm install --production
RUN npm install /tmp/inc
RUN rm -rf /tmp/inc
CMD ["npm", "start"]
My guess is you don't use --unsafe-perm npm key. Try change npm install to:
RUN npm install --production --unsafe-perm

Resources