'spawnSync('npm', ['install']) gives [Error: spawnSync npm ENOENT]

I am having an issue with spawnSync is giving me ENOENT with simple "npm install". Can someone please help me?

======= NODE SCRIPT ==========

var child = require('child_process').spawnSync('npm', ['install']); console.log(child.error);

===== OUTPUT ========== [Error: spawnSync npm ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'spawnSync npm', path: 'npm', spawnargs: [ 'install' ]

only on windows but not on OS X.

  • This happens on
    • windows 7 x64
    • node version: 4.4.3
    • npm version: 2.15.1


Solution 1:[1]

I figured out the issue. On Windows, some commands need to be suffixed with .cmd in order to work. In this example, this updated command works for me: require('child_process').spawnSync('npm.cmd', ['install']);

Solution 2:[2]

Or you can use cross-spawn to make it work cross-platform

Solution 3:[3]

I solved this by specifying the shell to be the v1 32-bit Powershell executable.

I don't know if there is a more reliable method of getting the Powershell executable path, but this is what I did:

const { spawnSync } = require('child_process')
const path = require('path')

const shell = process.platform === 'win32' 
  ? 'C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe' 
  : undefined

const r0 = spawnSync('pnpm', ['run', 'build'], {
  cwd: path.join(__dirname, '..', 'projects', 'project-name'),
  env: process.env,
  stdio: 'inherit',
  shell
})

if (r0.error) {
  console.log(r0.error)
  process.exit(1)
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henry Zou
Solution 2 Qiulang
Solution 3 David Alsh