Reputation: 87793
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: [email protected] (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for [email protected]: 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
.
Upvotes: 3
Views: 1780
Reputation: 87793
As it turns out, installing with snap was a bad idea.
It has auto-updates, which is not good for production.
The issue above for some reason only happens in snap environment.
That being said, I'd be interested in alternative answers that solve the issue in snap environment, as there may be legitimate uses of snap and the automation I was trying to run.
A better way to install recent versions of Node.js on Linux: https://github.com/nodesource/distributions/blob/master/README.md#installation-instructions
also: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04 (option 2)
Now having installed Node.js a normal way, child_process.exec('npm', console.log)
gives output as you would expect.
Upvotes: 2