Skip to content

Commit ecd2d23

Browse files
authored
fix: don't go into global mode if aliased to npmg (#7842)
BREAKING CHANGE: npm will no longer switch to global mode if aliased to "npmg" or "npm-g" etc. [This code](03bd669) is a remnant from when npm defined `bin` entries for itself that included `npm_g` and `npm-g`. npm no longer defines these, and this code should have been removed when those entries were removed. To utilize this today one would have to manually alias npm themselves. What this code does today in practice is make local development very tricky, because if you (like me) use git worktrees, and have a worktree directory that ends in "g", npm will be in global mode when you invoke it as `node .`. This is very "magical" behavior and not at all intuitive. It's best if this just goes away. `npm -g` is explicit and does not require npm trying to guess if you really wanted to be in global mode or not.
1 parent 62c71e5 commit ecd2d23

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

lib/cli/entry.js

-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ module.exports = async (process, validateEngines) => {
66
// leak any private CLI configs to other programs
77
process.title = 'npm'
88

9-
// if npm is called as "npmg" or "npm_g", then run in global mode.
10-
if (process.argv[1][process.argv[1].length - 1] === 'g') {
11-
process.argv.splice(1, 1, 'npm', '-g')
12-
}
13-
149
// Patch the global fs module here at the app level
1510
require('graceful-fs').gracefulify(require('node:fs'))
1611

test/lib/cli/entry.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ const cliMock = async (t, opts) => {
3535
}
3636
}
3737

38-
t.test('print the version, and treat npm_g as npm -g', async t => {
38+
t.test('print the version ', async t => {
3939
const { logs, cli, Npm, outputs, exitHandlerCalled } = await cliMock(t, {
40-
globals: { 'process.argv': ['node', 'npm_g', 'root'] },
40+
globals: { 'process.argv': ['node', 'npm', 'root'] },
4141
})
4242
await cli(process)
4343

44-
t.strictSame(process.argv, ['node', 'npm', '-g', 'root'], 'system process.argv was rewritten')
44+
t.strictSame(process.argv, ['node', 'npm', 'root'], 'system process.argv was rewritten')
4545
t.strictSame(logs.verbose.byTitle('cli'), ['cli node npm'])
4646
t.strictSame(logs.verbose.byTitle('title'), ['title npm root'])
47-
t.match(logs.verbose.byTitle('argv'), ['argv "--global" "root"'])
47+
t.match(logs.verbose.byTitle('argv'), ['argv "root"'])
4848
t.strictSame(logs.info, [`using npm@${Npm.version}`, `using node@${process.version}`])
4949
t.equal(outputs.length, 1)
5050
t.match(outputs[0], dirname(process.cwd()))

0 commit comments

Comments
 (0)