Jenkins build issue - npm ERR! Your cache folder contains root-owned files - node.js

I am trying to build a small node app on my Jenkins pipeline, which is running in a virtual machine. cross this error:
+ npm install
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /.npm
npm ERR! errno EACCES
npm ERR!
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR!
npm ERR! To permanently fix this problem, please run:
npm ERR! sudo chown -R 111:120 "/.npm"
Running sudo chown -R 111:120 "/.npm" doesn`t help since it says:
chown: cannot access '/.npm': No such file or directory
And, as per my understanding, runs in a local context, when the problem is actually from the container perspective. I`ve tried to add the command above on my Docker and Jenkinsfile as well, to no avail. Below is my public repo:
Node app deploy on github

npm install --cache=".YourCustomCacheDirectoryName"
works perfectly fine, reason for this is your docker user isn't allowed to write in / ( root directory )
its not that a directory already exist at /.npm its that, your script is trying to create a directory at / which is not accessible for your user
you can either put
agent {
docker {
image 'node:latest'
args '-u root:root'
}
}
or just tell npm to use your custom cache directory

I had the same issue and fixed it by setting the npm cache directory to ENV variable in Dockerfile.
Add this to Dockerfile:
ENV npm_config_cache /home/node/app/.npm

As far as I can remember ,just updating npm version and deleting the whole project did the trick.

Related

Your cache folder contains root-owned files

Trying to run npm ci on a git deployment branch for my website but receiving the following error;
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /home/storm/.npm
npm ERR! errno -13
npm ERR!
npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR!
npm ERR! To permanently fix this problem, please run:
npm ERR! sudo chown -R 1010:1010 "/home/storm/.npm"
I tried running the command it suggests but this doesn't work because there's no such file or directory of "/home/storm/.npm"
I can't find any answers online! Please help
There are multiple ways to solve the described issue. It depends on how you run npm.
Option 1:
Adapt the access rights on the npm cache files / folder. Just run the suggested command where 'storm' is your user name and '1010' are your user ID and group ID. (User ID and group ID sometimes differ.)
sudo chown -R 1010:1010 "/home/storm/.npm"
Option 2:
Remove the npm cache by running the following command:
sudo npm cache clean --force
Option 3:
Instruct npm to user another directory for caching (avoid using the default npm cache folder). Run the following command before running npm.
export npm_config_cache=/path/to/cache
In my case I ran yarn / npm in a CI/CD pipeline on a Jenkins agent and faced the same issue. Option 3 worked for me. I adapted my pipeline and replaced the command
gradle testAndCoverageWithYarn
with
export npm_config_cache=npm-cache && gradle testAndCoverageWithYarn
(The gradle task testAndCoverageWithYarn calls yarn / npm.)
I had this issue and this command worked for me:
sudo npm cache clean --force
If this issue sill occured try :
sudo chown -R 1010:1010 "/home/storm/.npm"
Good Luck ;-) I hope it works for You! Watch out for typos ..

NPM not working on Virtualbox shared folder under Ubuntu on a Windows 10 host

I'm trying to set-up a development environment on a Virtualbox machine using Vagrant. I'm using Windows 10 as a host machine, and ubuntu/bionic64 as a guest machine.
My package.json file is located under a shared directory between the host machine and the guest machine. However whenever I run npm install --no-bin-links I do get the following error:
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /home/vagrant/www/wp-content/plugins/my-plugin/node_modules/array-initial/node_modules/is-number/package.json.1640612897
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/home/vagrant/www/wp-content/plugins/my-plugin/node_modules/array-initial/node_modules/is-number
/package.json.1640612897'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /home/vagrant/.npm/_logs/2021-01-19T05_31_47_272Z-debug.log
Inside the guest machine, if I copy the package.json and package-lock.json into another directory that is not shared, and then run npm install everything works perfectly.
mkdir /tmp/test/
cp package.json package-lock.json /tmp/test/
cd /tmp/test/ && npm install
# NPM Packages are installed successfully!
I think this error is specific to Windows, as I've been a user of Ubuntu for years, and never had a similar issue. I only started using Windows a week ago.
The strange thing is, I also have another development environment on the same machine, and NPM works just fine, without any issues on shared folders!
I used the --no-bin-links, so I don't think the issue has anything to do with the symbolic links.
Looking into the strace output, I can't see when this file node_modules/array-initial/node_modules/is-number /package.json.1640612897 was created.
NPM log
Strace log

Why "npm install -g grunt-cli" doesn't work correctly?

I am having trouble using grunt in my projects. I am setting up an macos computer with version 11.0.1.
The project files (local) are in principle correct, since I use it on another computer and it works fine there.
I have been trying to solve this error for more than two days, the problem is that the local configuration does not work correctly, since I get the message ~ bash: grunt command not found when trying to initialize grunt with the grunt or grunt watch command
When trying to install grunt-cli globally I get this error:
npm ERR! code ENOENT
npm ERR! syscall lstat
npm ERR! path /Users/bertanicolau/.npm-packages
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, lstat '/Users/bertanicolau/.npm-packages'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/bertanicolau/.npm/_logs/2020-11-23T17_06_23_295Z-debug.log
The .npm-packages folder does not exist in this location, instead the .npm folder does exist, I don't know why it is looking for another.
I've tried clearing the npm cache, I've also tried deleting the package-lock.json file and changing paths on .bash-profile but none of this seems to work.
Can it be a npm or node version bug?
$ npm -v && node -v
7.0.8
v15.2.1
Thanks!
In one of your .npmrc files, you probably have prefix=/Users/trott/.npm-packages. If so, leave it and create the directory with mkdir ~/.npm-packages. Or set it to a different directory. Or remove the entry entirely.
The places to look for the .npmrc file that may be causing this to happen:
per-project config file (/path/to/my/project/.npmrc)
per-user config file (~/.npmrc)
global config file ($PREFIX/etc/npmrc)
npm builtin config file (/path/to/npm/npmrc)

npm install no longer possible after deleting module directory

So I wanted to help out, and forked a Node-RED module to make some small contribution, but then got lost messing around with how to get Node-RED to run my fork (for testing) rather than the packaged version. Then I learned about npm's ability to install from a Github URL, which is nice, but it wouldn't work because I had already installed the module. Then I found I couldn't uninstall it either
npm uninstall original-module
npm ERR! path /home/nodered/.node-red/node_modules/original-module/package.json
npm ERR! code ELOOP
npm ERR! errno -40
npm ERR! syscall open
npm ERR! ELOOP: too many symbolic links encountered, open '/home/nodered/.node-red/node_modules/original-module/package.json'
so in frustration I deleted the module's directory, only to find myself unable to move forwards, or backwards. npm install https://github.com/me/forkedmodule fails with
npm ERR! code 128
npm ERR! Command failed: /usr/bin/git clone --depth=1 -q -b master https://github.com/me/forkedmodule.git /root/.npm/_cacache/tmp/git-clone-d31dc53b
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-d31dc53b': Permission denied
npm ERR!
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-08-22T01_07_18_257Z-debug.log
and trying to install the original with npm install original-module fails with
npm ERR! code ENOLOCAL
npm ERR! Could not install from "node_modules/original-module" as it does not contain a package.json file.
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2017-08-22T01_09_56_127Z-debug.log
As you've probably guessed, I'm pretty new to Node.js, Node-RED and npm, but I haven't been able to help myself, so have to ask for help! It seems nonsensical to me that npm appears to be looking for a local copy of the package.json file belonging to the package I'm trying to install?
Edit: I should add that the above commands were all run as root.
Ok, found a solution, not sure if it's the best one though!
In the "project" directory (/home/nodered/.nodered in my case) there's a package-lock.json file which lists the packages used by the project. In there I found an entry for the original-module which I removed, after which I was able to install the package "normally" with npm install original-module.
Then to solve the permission issue when installing from a GitHub URL, I found I needed to impersonate the "nodered" user with su - nodered (it's a password-less user in my case). As "nodered" npm install https://github.com/me/forkedmodule succeeded without any error. A little curious since I was trying to do this as root before, and got a permission error, but there you go. I also took the opportunity to chown -R the project's node_modules directory to nodered:nodered as I could see that was a bit of mess.
In future, I will try to remember to su - to my nodered user first, and navigating to the project root (again, /home/nodered/.node-red in my case) before running npm install anything. Hopefully that will avoid any further issues. Oh and a note to anyone else who just wants to contrib some quick changes to a module hosted on GitHub: to "pull" in changes you simply run npm install https://github.com/me/forkedmodule again - npm doesn't appear to provide any dedicated "update" method.
I had the same issue, in my case the solution was to delete the package-lock.json, delete node_modules, then reinstall npm modules.

How can I add a .npmrc file?

I installed node on my Mac OS Sierra. I use Windows at my work so there I have a .npmrc file in the node folder but I don't seem to find that in mac.
The problem is I want to add a registry of the format
"scope=rohit-project#rohit-aquila:registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=some-token"
How do I add it so that I can install the dependencies and modules for my project by running npm install on MAC OS Sierra.
I created a .npmrc file simply and added the above code...and therefater running npm install I get the following error
rohitsrivastava$ npm install
npm ERR! Darwin 16.4.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install"
npm ERR! node v7.7.3
npm ERR! npm v4.1.2
npm ERR! code E404
npm ERR! 404 Not found : #rohit-project/notes
npm ERR! 404
npm ERR! 404 '#rohit-project/notes' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 It was specified as a dependency of '#rohit-project/mega'
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
There are a few different points here:
Where is the .npmrc file created.
How can you download private packages
Running npm config ls -l will show you all the implicit settings for npm, including what it thinks is the right place to put the .npmrc (the field userconfig), as this is environment/operating system dependant. But if you have never logged in (using npm login) it will be empty. Simply log in to create it.
To just output the path prop, issue npm config get userconfig.
Another thing is #2 - downloading private packages. You can actually do that by putting a .npmrc file in the NPM package's root. It will then be used by NPM when authenticating. It also supports variable interpolation from your shell so you could do stuff like this:
; Get the auth token to use for fetching private packages from our private scope
; see http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
; and also https://docs.npmjs.com/files/npmrc
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Pointers
http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
https://docs.npmjs.com/files/npmrc
In MacOS Catalina 10.15.5 the .npmrc file path can be found at
/Users/<user-name>/.npmrc
Open in it in (for first time users, create a new file) any editor and copy-paste your token. Save it.
You are ready to go.
Note:
As mentioned by #oligofren, the command npm config ls -l will npm configurations. You will get the .npmrc file from config parameter userconfig
This issue is because of you having some local or private packages.
For accessing those packages you have to create .npmrc file for this issue. Just refer the following link for your solution. https://nodesource.com/blog/configuring-your-npmrc-for-an-optimal-node-js-environment
In my case, updating my npm version helped me. So just to be sure, make sure your npm is up to date.
npm install -g npm#latest
Assuming you are using VSTS run vsts-npm-auth -config .npmrc to generate new .npmrc file with the auth token

Resources