Skip to content

Commit 6e8f93b

Browse files
docs: title match content (#2574)
1 parent 2ef5fb8 commit 6e8f93b

File tree

2 files changed

+88
-20
lines changed

2 files changed

+88
-20
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Force npm to use global installed node-gyp
2+
3+
[Many issues](https://github.com/nodejs/node-gyp/labels/ERR%21%20node-gyp%20-v%20%3C%3D%20v5.1.0) are opened by users who are
4+
not running a [current version of node-gyp](https://github.com/nodejs/node-gyp/releases).
5+
6+
`npm` bundles its own, internal, copy of `node-gyp`. This internal copy is independent of any globally installed copy of node-gyp that
7+
may have been installed via `npm install -g node-gyp`.
8+
9+
Generally, npm's library files are installed inside your global "node_modules", where npm is installed (run `npm prefix` and add `lib/node_modules`, or just `node_modules` for Windows). There are some exceptions to this. Inside this global `node_modules/` there will be an `npm/` directory and inside this you'll find a `node_modules/node-gyp/` directory. So it may look something like `/usr/local/lib/node_modules/npm/node_modules/node-gyp/`. This is the version of node-gyp that ships with npm.
10+
11+
When you install a _new_ version of node-gyp outside of npm, it'll go into your global node_modules, but not under the `npm/node_modules`. So that may look like `/usr/local/lib/node_modules/node-gyp/`. It'll have the `node-gyp` executable linked into your `PATH` so running `node-gyp` will use this version.
12+
13+
The catch is that npm won't use this version unless you tell it to, it'll keep on using the one you have installed. You need to instruct it to by setting the `node_gyp` config variable (which goes into your `~/.npmrc`). You do this by running the `npm config set` command as below. Then npm will use the command in the path you supply whenever it needs to build a native addon.
14+
15+
**Important**: You also need to remember to unset this when you upgrade npm with a newer version of node-gyp, or you have to manually keep your globally installed node-gyp to date. See "Undo" below.
16+
17+
## Linux and macOS
18+
```
19+
npm install --global node-gyp@latest
20+
npm config set node_gyp $(npm prefix -g)/lib/node_modules/node-gyp/bin/node-gyp.js
21+
```
22+
23+
`sudo` may be required for the first command if you get a permission error.
24+
25+
## Windows
26+
27+
### Windows Command Prompt
28+
```
29+
npm install --global node-gyp@latest
30+
for /f "delims=" %P in ('npm prefix -g') do npm config set node_gyp "%P\node_modules\node-gyp\bin\node-gyp.js"
31+
```
32+
33+
### Powershell
34+
```
35+
npm install --global node-gyp@latest
36+
npm prefix -g | % {npm config set node_gyp "$_\node_modules\node-gyp\bin\node-gyp.js"}
37+
```
38+
39+
## Undo
40+
**Beware** if you don't unset the `node_gyp` config option, npm will continue to use the globally installed version of node-gyp rather than the one it ships with, which may end up being newer.
41+
42+
```
43+
npm config delete node_gyp
44+
npm uninstall --global node-gyp
45+
```

docs/Updating-npm-bundled-node-gyp.md

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,63 @@ not running a [current version of node-gyp](https://github.com/nodejs/node-gyp/r
66
`npm` bundles its own, internal, copy of `node-gyp`. This internal copy is independent of any globally installed copy of node-gyp that
77
may have been installed via `npm install -g node-gyp`.
88

9-
Generally, npm's library files are installed inside your global "node_modules", where npm is installed (run `npm prefix` and add `lib/node_modules`, or just `node_modules` for Windows). There are some exceptions to this. Inside this global `node_modules/` there will be an `npm/` directory and inside this you'll find a `node_modules/node-gyp/` directory. So it may look something like `/usr/local/lib/node_modules/npm/node_modules/node-gyp/`. This is the version of node-gyp that ships with npm.
9+
This means that while `node-gyp` doesn't get installed into your `$PATH` by default, npm still keeps its own copy to invoke when you
10+
attempt to `npm install` a native add-on.
1011

11-
When you install a _new_ version of node-gyp outside of npm, it'll go into your global node_modules, but not under the `npm/node_modules`. So that may look like `/usr/local/lib/node_modules/node-gyp/`. It'll have the `node-gyp` executable linked into your `PATH` so running `node-gyp` will use this version.
12+
Sometimes, you may need to update npm's internal node-gyp to a newer version than what is installed. A simple `npm install -g node-gyp`
13+
_won't_ do the trick since npm will still continue to use its internal copy over the global one.
1214

13-
The catch is that npm won't use this version unless you tell it to, it'll keep on using the one you have installed. You need to instruct it to by setting the `node_gyp` config variable (which goes into your `~/.npmrc`). You do this by running the `npm config set` command as below. Then npm will use the command in the path you supply whenever it needs to build a native addon.
15+
So instead:
1416

15-
**Important**: You also need to remember to unset this when you upgrade npm with a newer version of node-gyp, or you have to manually keep your globally installed node-gyp to date. See "Undo" below.
17+
## Version of npm
1618

17-
## Linux and macOS
18-
```
19-
npm install --global node-gyp@latest
20-
npm config set node_gyp $(npm prefix -g)/lib/node_modules/node-gyp/bin/node-gyp.js
19+
We need to start by knowing your version of `npm`:
20+
```bash
21+
npm --version
2122
```
2223

23-
`sudo` may be required for the first command if you get a permission error.
24+
## Linux, macOS, Solaris, etc.
2425

25-
## Windows
26+
Unix is easy. Just run the following command.
2627

27-
### Windows Command Prompt
28+
If your npm is version ___7___, do:
29+
```bash
30+
$ npm explore npm/node_modules/@npmcli/run-script -g -- npm_config_global=false npm install node-gyp@latest
2831
```
29-
npm install --global node-gyp@latest
30-
for /f "delims=" %P in ('npm prefix -g') do npm config set node_gyp "%P\node_modules\node-gyp\bin\node-gyp.js"
32+
33+
Else if your npm is version ___less than 7___, do:
34+
```bash
35+
$ npm explore npm/node_modules/npm-lifecycle -g -- npm install node-gyp@latest
3136
```
3237

33-
### Powershell
38+
If the command fails with a permissions error, please try `sudo` and then the command.
39+
40+
## Windows
41+
42+
Windows is a bit trickier, since `npm` might be installed to the "Program Files" directory, which needs admin privileges in order to
43+
modify on current Windows. Therefore, run the following commands __inside a `cmd.exe` started with "Run as Administrator"__:
44+
45+
First we need to find the location of `node`. If you don't already know the location that `node.exe` got installed to, then run:
46+
```bash
47+
$ where node
3448
```
35-
npm install --global node-gyp@latest
36-
npm prefix -g | % {npm config set node_gyp "$_\node_modules\node-gyp\bin\node-gyp.js"}
49+
50+
Now `cd` to the directory that `node.exe` is contained in e.g.:
51+
```bash
52+
$ cd "C:\Program Files\nodejs"
3753
```
3854

39-
## Undo
40-
**Beware** if you don't unset the `node_gyp` config option, npm will continue to use the globally installed version of node-gyp rather than the one it ships with, which may end up being newer.
55+
If your npm version is ___7___, do:
56+
```bash
57+
cd node_modules\npm\node_modules\@npmcli\run-script
58+
```
4159

60+
Else if your npm version is ___less than 7___, do:
61+
```bash
62+
cd node_modules\npm\node_modules\npm-lifecycle
4263
```
43-
npm config delete node_gyp
44-
npm uninstall --global node-gyp
64+
65+
Finish by running:
66+
```bash
67+
$ npm install node-gyp@latest
4568
```

0 commit comments

Comments
 (0)