Skip to content

Commit f4c9a68

Browse files
committed
Fix logging module in newer versions of npm
1 parent a430fc8 commit f4c9a68

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/commands/npm/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { spawn } from 'child_process'
1+
import { spawn, execSync } from 'child_process'
22
import { fileURLToPath } from 'url'
33

44
const description = 'npm wrapper functionality'
@@ -7,10 +7,15 @@ const description = 'npm wrapper functionality'
77
export const npm = {
88
description,
99
run: async (argv, _importMeta, _ctx) => {
10+
const npmVersion = execSync('npm -v').toString()
1011
const wrapperPath = fileURLToPath(new URL('../../shadow/npm-cli.cjs', import.meta.url))
1112
process.exitCode = 1
1213
spawn(process.execPath, [wrapperPath, ...argv], {
13-
stdio: 'inherit'
14+
stdio: 'inherit',
15+
env: {
16+
...process.env,
17+
NPM_VERSION: npmVersion
18+
}
1419
}).on('exit', (code, signal) => {
1520
if (signal) {
1621
process.kill(process.pid, signal)

lib/shadow/npm-injection.cjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,17 @@ function findRoot (filepath) {
268268
}
269269
const npmDir = findRoot(path.dirname(npmEntrypoint))
270270
const arboristLibClassPath = path.join(npmDir, 'node_modules', '@npmcli', 'arborist', 'lib', 'arborist', 'index.js')
271-
const npmlog = require(path.join(npmDir, 'node_modules', 'npmlog', 'lib', 'log.js'))
271+
272+
const npmVersion = process.env.NPM_VERSION.split('.')
273+
let npmlog
274+
275+
if(npmVersion[0] === '10' && npmVersion[1] >= '7'){
276+
const { log } = require(path.join(npmDir, 'node_modules', 'proc-log', 'lib', 'index.js'))
277+
npmlog = log
278+
} else {
279+
npmlog = require(path.join(npmDir, 'node_modules', 'npmlog', 'lib', 'log.js'))
280+
}
281+
272282
/**
273283
* @type {import('pacote')}
274284
*/

0 commit comments

Comments
 (0)