NPM Error : Error: EROFS: read-only file system, mkdir '/npm'
Asked Answered
N

7

11

I created an index.js, index.html and package.json file and I wanted to add express. When I write 'npm install express' in the terminal, I'm getting the error below. So far, I tried 'sudo npm install -g express', deleting node and npm completely and re-install. I also went through other questions over here but none of them worked out for me. Whatever I write with npm I get the same error. (I'm using macOS)

Do you have any suggestions?

This is the error I am getting:

Error: EROFS: read-only file system, mkdir '/npm'

TypeError: Cannot read property 'loaded' of undefined

    at exit (/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:97:27)

    at errorHandler (/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:216:3)

    at /usr/local/lib/node_modules/npm/bin/npm-cli.js:78:20

    at cb (/usr/local/lib/node_modules/npm/lib/npm.js:225:22)

    at /usr/local/lib/node_modules/npm/lib/npm.js:263:24

    at /usr/local/lib/node_modules/npm/lib/config/core.js:81:7

    at Array.forEach (<anonymous>)

    at /usr/local/lib/node_modules/npm/lib/config/core.js:80:13

    at f (/usr/local/lib/node_modules/npm/node_modules/once/once.js:25:25)

    at afterExtras (/usr/local/lib/node_modules/npm/lib/config/core.js:171:20)

/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:97

  var doExit = npm.config.loaded ? npm.config.get('_exit') : true

                          ^

TypeError: Cannot read property 'loaded' of undefined

    at exit (/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:97:27)

    at process.errorHandler (/usr/local/lib/node_modules/npm/lib/utils/error-handler.js:216:3)

    at process.emit (events.js:223:5)

    at process._fatalException (internal/process/execution.js:150:25)
Nervous answered 1/2, 2020 at 19:3 Comment(0)
G
10

The error means that you have no permission to install anything with npm at the path /npm.
To verify this run npm root -g. This should return /npm.

Note that /npm ist not the default installation path on MacOS (see https://mcmap.net/q/158412/-where-should-my-npm-modules-be-installed-on-mac-os-x).

Checkout your npm configuration file and change the default installation path to something where you do have write access. You can also use npm itself to change the default installation path.

Gorged answered 1/2, 2020 at 20:50 Comment(0)
G
1

mv ~/.npmrc ~/.npmrc.old

npm prefix -g

this might work

Greatniece answered 24/2, 2022 at 4:16 Comment(0)
D
1

In my case it turned out to be a problem with the cache entry in the ~/.npmrc file. Apparently one of the sub-projects I have worked on required the line:

cache=/build/package_cache/npm

But when run in another sub-project it seems to have caused npm to not be able to create the /build directory.

Commenting out that line worked around the problem.

Dyann answered 13/3, 2023 at 21:1 Comment(0)
M
1

I had error EROFS: read-only file system, mkdir '/.npm' while I was trying to install packages.

Then I used npm config list --json and found only thing used this path is "cache": "/.npm".

So I changed cache by npm config set cache '/tmp', then installation worked.

Mosera answered 17/7, 2023 at 7:56 Comment(0)
P
1

This Issue might be also caused by dual-boot where windows hasn't been shutdown properly. You can spin up windows again and then Shutdown properly.

You can either do shutdown /s /f /t 0 /full or go to control panel -> power-settings-> what power button should do -> change unavailable settings and turn on fast-boot. and boot up your secondary boot. This could potentially fix the issue.

Porcia answered 7/7 at 16:31 Comment(0)
U
0

I encountered a similar issue when trying to write to a file because of a leading slash that I had in front of the filename so using the following line causes the issue :

fs.writeFileSync("/output.txt", JSON.stringify(data), "utf8");

While the issue is gone with the below one (after removing the leading slash):

fs.writeFileSync("output.txt", JSON.stringify(data), "utf8");
Uncertainty answered 20/3 at 11:45 Comment(0)
E
-1

In my case, I deleted ~/.local/lib, then re-ran the npm install -g command. No more error.

Earthlight answered 9/2, 2023 at 8:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.