Does "npm install" look for packages in local cache first? - npm

I was trying to figure out how local cache is fitted in npm install flow. I did the following:
npm cache clear --force
npm cache rd
npm install react#16.10.1
I disconnected from the internet
I removed the node_modules directory
npm install react#16.10.1
The package got installed and a package-lock.json was created successfully; however the following warning in the console made me confused:
Using stale data from https://registry.npmjs.org/ because the host is inaccessible -- are you offline?
npm WARN registry Using stale data from https://registry.npmjs.org/ due to a request error during revalidation.
...
...
Why did npm try to connect to the registry at all while the package does exist in the local cache? and
What was npm trying to get from the registry? Did it try to download the whole package or it just wanted to check some metadata like hash or so?

Related

JFrog private registry fails to npm install

I'm having issues with JFrog NPM private repositories.
In our CI we configure the npm private registry and then do npm install like this:
- npm_private_registry https://DOMAIN.jfrog.io/DOMAIN/api/npm/npm-virtual/ $TOKEN
- npm install
This code works perfectly fine when we use the node:12.13.1 Docker image.
Our problem is that as soon as we change the image to a newer one, let's say node:12.16.1 then the npm install command fails.
We then get the following errors:
$ npm install
(wd=/builds/DOMAIN/APP)
npm WARN tar ENOENT: no such file or directory, open '/builds/DOMAIN/APP/node_modules/.staging/react-icons-921e2ffa/go/package.json'
npm WARN tar ENOENT: no such file or directory, open '/builds/DOMAIN/APP/node_modules/.staging/intl-58a6f707/locale-data/jsonp/gsw-FR.js'
...
...
...
npm ERR! Response timeout while trying to fetch https://DOMAIN.jfrog.io/DOMAIN/api/npm/npm-virtual/next (over 30000ms)
We tried various approaches already, but haven't been able to figure anything out just yet.
Does anyone have an idea where to start?
The issue seems quite generic. How is it possible to get timeouts on one version of the node image and no issues on another?
Thank you!
Seems like this link https://github.com/npm/cli/issues/1151 offers some explanation. The timeout got hardcoded down to 30000 ms in npm 16.4.0 via npm-registry-fetch. This has now been reverted to not timeout again https://github.com/npm/npm-registry-fetch/commit/fc5d94c39ca218d78df77249ab3a6bf1d9ed9db1. This fix is in 6.14.5 of npm https://github.com/npm/cli/releases/tag/v6.14.5.
In terms of default npm with node versions this means, releases 12.16.2 and 12.16.3 of node contain this problem via npm 6.14.4.

How do I install react and react-redux onto my project?

Good afternoon,
I am trying to install react and react-redux onto my project, but I keep getting errors that I don't understand.
$ npx create-react-app learn-redux
npm ERR! code ENOLOCAL
npm ERR! Could not install from "Phelps\AppData\Roaming\npm-cache\_npx\13464" 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\Taylor Phelps\AppData\Roaming\npm-cache\_logs\2020-02-08T20_44_17_568Z-debug.log
Install for [ 'create-react-app#latest' ] failed with code 1
Could not install from "Phelps\AppData\Roaming\npm-cache\_npx\13464" as it does not contain a package.json file.
I am reading the error, but the problem is...that folder does not exist on my computer and I've checked in the terminal for npm and npx and both are installed on my computer. Does anyone have any ideas on why this may be happening and how to fix it?
Thank you.
According to this Github issue npx has trouble running on Windows if the user has a space in their name and from the error message I'd guess that is the case here. One of the suggested fixes is to set your npm-cache path to a folder without a space in it:
npm config set cache C:\tmp\nodejs\npm-cache --global
Then try running your command again.
Install the project in another directory except C partition :
npx create-react-app learnredux
Then :
npm i react-redux --save
This should work.
The problem is the npm cache by the path of the error. Try running:
npm cache verify
Verify the contents of the cache folder, garbage collecting any
unneeded data, and verifying the integrity of the cache index and all
cached data.
(from https://docs.npmjs.com/cli-commands/cache.html)

npm - tarball data for material-design-icons seems to be corrupted

I'm having this error while running a npm install material-design-icons#3.0.1:
tarball data for material-design-icons#3.0.1 (sha1-mnHEh0chjrylHlGmbaaCA4zct78=) seems to be corrupted
npm ERR! path D:\speech-analytics\node_modules\.staging\material-design-icons-7d5a1f73\action\drawable-xxhdpi\ic_assignment_ind_white_48dp.png
npm ERR! code EPERM
npm ERR! errno -4048
npm ERR! syscall unlink
npm ERR! Error: EPERM: operation not permitted, unlink 'D:\\speech-analytics\node_modules\.staging\material-design-icons-7d5a1f73\action\drawable-xxhdpi\ic_assignment_ind_white_48dp.png'
npm ERR! { Error: EPERM: operation not permitted, unlink 'D:\\speech-analytics\node_modules\.staging\material-design-icons-7d5a1f73\action\drawable-xxhdpi\ic_assignment_ind_white_48dp.png'
Here it's documented as a bug, but still without an answer nor a fix.
I've tried to reinstall node, upgraded to latest npm version (currently running 6.4.1), did a cache clean --force, tried a npm install --no-optional, removed package-lock.json, removed npm & npm-cache folders from AppData directory, running everything as Administrator, but still no luck.
I even tried with material-design-icons#3.0.0 but the error remains.
If I navigate to the folder that appears in the log (node_modules\.staging\material-design-icons-7d5a1f73\action\drawable-xxhdpi), it's empty, and is the only folder that exists in the entire node_modules directory. I can delete that dir without any problems, so it does not seem to be a permissions/lock issue.
Any suggestions?
Finally, I got this fixed by:
Removing node_modules folder
Running npm update
Running npm install
As far I understand, the npm update should have updated the package.json file, but all dependencies kept the same versions as we had it before.
I resolved this with the command: npm cache verify which output:
Cache verified and compressed (C:\Programs\DCPS\npm-cache\_cacache):
Content verified: 1344 (164824963 bytes)
Content garbage-collected: 1 (3491551 bytes)
Index entries: 1522
Finished in 8.187s
The line that stands out to me is: Content garbage-collected: 1 (3491551 bytes)
Does this sort of thing happen because a new version of a package is published to npmjs without a version bump?
No need to run npm update (I didn't want to update any packages) or delete the entire node_modules folder. I solved this by
deleting package-lock.json
deleting node_modules\material-design-icons-xxxxxxx
running npm install again
If npm update is not a solution, and as deleting package-lock.json can bring issues of its own, I could solve it simply by:
deleting the node_modules/ folder
in package-lock.json, deleting the sections referencing to the corrupted package
running npm install again
If you are on windows env, I fixed it by running the cmd as administrator
You need to confirm whether the Typescript is installed and after installing typescript it worked for me
running the below comment will show the typescript version
tsc -v
If it shows some error install the typescript
npm install -g typescript
If then typescript is installed you can try checking the Angular Cli version
ng --version
If it shows some error then install Angular Cli Ref: https://cli.angular.io/
npm install -g #angular/cli
" If you are on windows env, I fixed it by running the cmd as administrator "
This worked for me. However, chromedriver was not installed. So, i installed it separately using the command 'npm install chromedriver'.
None of the answers solved my problem, because in my case was the git. Maybe someone can have the same problem.
I had some dependencies from git in the project and my git was not working on the terminal. So fixing the path for git fixed it!
I had this in Bitbucket Pipeline when using a private package.
I was missing to install git in pipeline:
script:
- apk update && apk upgrade && apk add --no-cache bash git openssh # <- THIS
- npm ci --prefer-offline
Dependency was
"some-private-package": "git+ssh://git#bitbucket.org/workspace/some-private-package#v1.0.12",`
Well I could not resolve this problem with a lot of tries so I made the download of the github ZIP, unzip and install and it worked !
download material-design-icons from github
unzip to the directory of your project (or c:\tmp)
npm install ./material-design-icons
or
npm install c:/tmp/material-design-icons

How can I remove the npm registry from my machine?

I published a module to npm. When I uninstall it and try to reinstall it, this is always done from a cache ( it is done even if I'm not connected to the internet, I just get a warning ). I would like to try if my module installs correctly from the remote npm repository on a fresh npm registry.
I tried to remove Node.js and reinstall it, but it does not help. There is no "npm" in the list of my applications that I could remove, only Node.js is listed. This is on a Windows 10 machine.
Where is the npm registry located and how can I remove it entirely?
You have to clean your NPM cache and then clear your registry using the below commands.
npm cache clean
npm config delete registry
Thanks
npm config delete registry
This worked for me

access npm modules locally in case npm server is down

I am looking to store npm modules in a cache/locally in case the npm server is down and I cannot access the modules.
I hear npm-cache or npm-offline work well, but I'm not sure if they actually solve the problem I face - which is accessing an npm module locally.
Any recommendations?
Install offline-npm with
sudo npm install -g offline-npm
offline-npm will run a local http server with the content of your cache. Launch the server with:
offline-npm -n -d
You can then install packages from your cache with:
npm --registry http://localhost:4873 install <packagename>
To inspect the content of your cache run:
npm cache ls
You can also add a package to your cache without installing it with:
npm cache add <package>
For more information, read the official documentation of offline-npm.