[npm/cli] [BUG] npm/npx crashes if stdout is Node's 'pipe' (#2114)
DRANK

# Current Behavior Executing npm or npx commands from within a parent process that sets the child's stdout to "pipe" causes npm/npx to crash. ## Expected Behavior npm/npx to detect that this condition exists and proceed without issue. ## Steps To Reproduce 1. Using Linux with Node 12 in a folder containing a valid package.json. 2. With rimraf already installed as a (optionally dev) dependency. I chose rimraf for this example as it is well-known and simple, but this reproduces with any command. I was originally having the problem with husky-run. 3. Execute the following: ```sh echo 'const child = require("child_process"); child.execSync("npx --no-install rimraf x");' | node - ``` or any of the following ```sh echo 'const child = require("child_process"); child.execSync("npx --no-install rimraf x", {stdio:"pipe"});' | node - echo 'const child = require("child_process"); child.execSync("npx --no-install rimraf x", {stdio:["ignore", "pipe"]});' | node - ``` results in ```text child_process.js:674 throw err; ^ Error: Command failed: npx rimraf x at checkExecSyncError (child_process.js:635:11) at Object.execSync (child_process.js:671:15) at [stdin]:1:47 at Script.runInThisContext (vm.js:120:18) at Object.runInThisContext (vm.js:309:38) at Object.<anonymous> ([stdin]-wrapper:10:26) at Module._compile (internal/modules/cjs/loader.js:1015:30) at evalScript (internal/process/execution.js:94:25) at internal/main/eval_stdin.js:29:5 at Socket.<anonymous> (internal/process/execution.js:207:5) { status: 243, signal: null, output: [ null, Buffer(0) [Uint8Array] [], Buffer(0) [Uint8Array] [] ], pid: 247932, stdout: Buffer(0) [Uint8Array] [], stderr: Buffer(0) [Uint8Array] [] } ``` ## Workarounds ```sh echo 'const child = require("child_process"); child.execSync("npx --no-install rimraf x", {stdio:"inherit"});' | node - echo 'const child = require("child_process"); child.execSync("npx --no-install rimraf x", {stdio:"ignore"});' | node - echo 'const child = require("child_process"); child.execSync("npx --no-install rimraf x", {stdio:[0,1]});' | node - echo 'const child = require("child_process"); child.execSync("npx --no-install rimraf x", {stdio:[undefined, "ignore"]});' | node - ``` ## Environment - OS: Alpine 3.11.6 and Ubuntu 20.04 - Node: 14.2.0 and 12.19.0, respectively - npm: tested on both 6.14.4 and 7.0.7, respectively Under Alpine's Node 14 I was unable to reproduce, likely due to Node having changed behavior. These two configurations are what I have at hand. I did first detect the issue on my Ubuntu 20.04 with Node 12 and npm 6 setup before I upgraded npm to 7. ## Notes This fundamental problem is the source of several bug reports over the years in many repositories, so I have no doubt that an issue exists that already documents this bug - but I've been unable to find it.

github.com
Related Topics: JavaScript
2 comments
  • efm-langserver内部でnpx経由でprettierを呼び出した場合、npm/cliのバグ?でクラッシュするぽい。よく読んでないけど以下が関係するかも。端末割当とかがワークアラウンドになったりしないかな。 github.com/npm/cli/issues…