I've established that running npm install manually works:
# rm -r node_modules/
# npm install
npm WARN nextjs No repository field.
npm WARN nextjs No license field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.3.2 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.3.2: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
added 275 packages from 259 contributors and audited 276 packages in 14.233s
43 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
# echo $?
0
As you see, exit code 0 when doing manually. No error.
In my case, package.json looks like this:
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"next": "^10.2.0",
"nprogress": "^0.2.0",
"postgres": "^2.0.0-beta.5",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
"devDependencies": {
"@types/nprogress": "^0.2.0",
"@types/react": "^17.0.3",
"typescript": "^4.2.4"
}
}
However, doing it this way doesn't work as expected:
# rm -r node_modules
# node my_script.js
err = Error: Command failed: npm install
at ChildProcess.exithandler (child_process.js:308:12)
at ChildProcess.emit (events.js:315:20)
at maybeClose (internal/child_process.js:1048:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5) {
killed: false,
code: 243,
signal: null,
cmd: 'npm install'
}
stdout =
stderr =
with the following script, and I would have expected it to work: :-(
const child_process = require('child_process')
child_process.exec('npm install', function (err, stdout, stderr) {
console.log('err =', err)
console.log('stdout =', stdout)
console.log('stderr =', stderr)
})
It's the exact same command in a different context, but yields exit code 243 with no output!
But guess what! The modules all install correctly. Only the exit code and lack of output are bad.
For now I'm just going to ignore this particular exit code, but I'd much rather understand what's going on.
Why especially no output? Even child_process.exec('npm') by itself produces no output. Even wrapping in bash -c "... >> log 2>> log" produces no output to a log file. What can I do to fix this?
Even using spawn (instead of exec) is having same issue where npm produces no output. child_process.spawn('bash', ['-c', "npm >> log 2>> log"]).on('error', console.log) for both npm --help and npm install.