0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Winでspawn使ってnpmコマンド実行がうまくいかなかったときの対応

Posted at

Winでspawn使ってnpmコマンド実行がうまくいかなかったときの対応

とりあえずnpmのコマンド実行してみた

実行してみた
const { spawn } = require('child_process');
spawn('npm', ['-v']);  // Error: spawn npm ENOENT

なんでやねん。

どうやらそのままだと実行できない模様

Winはnpmではなくnpm.cmdとしないといけないと。

修正して再度実行してみる

修正して再実行
const { spawn } = require('child_process');
spawn('npm.cmd', ['-v']);

実行できた!

Win以外も考慮

process.platformで切り替え処理追加
const { spawn } = require('child_process');
const isWin = process.platform === 'win32';
spawn(isWin ? 'npm.cmd' : 'npm', ['-v']);

環境

  • Windows 11
  • Nodejs 19.7.0
  • npm 9.5.0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?