wasm-pack-plugin icon indicating copy to clipboard operation
wasm-pack-plugin copied to clipboard

Fix error spawn npm ENOENT on Windows

Open furrycoding opened this issue 1 year ago • 2 comments

On Windows, running webpack with this plugin added results in the following log:

> webpack

🧐  Checking for wasm-pack...

ℹ️  Installing wasm-pack 

⚠️ could not install wasm-pack globally when using npm, you must have permission to do this
[webpack-cli] Error: spawn npm ENOENT
    at ChildProcess._handle.onexit (node:internal/child_process:286:19)
    at onErrorNT (node:internal/child_process:484:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -4058,
  code: 'ENOENT',
  syscall: 'spawn npm',
  path: 'npm',
  spawnargs: [ 'install', '-g', 'wasm-pack' ]
}

This seems to be because, by default, spawn on Windows will only look for .exe or .com files (docs) However, npm is actually a .cmd file(similar to a .bat, an equivaent of a Linux shell script) This can be fixed in a cross-platform way by enabling the shell option in spawn

There was a previous PR #122 which fixed this problem, however, it was closed by the author on the same day without a reason

furrycoding avatar Mar 23 '24 08:03 furrycoding

I believe this pull request would also fix an EINVAL error (which is being silently swallowed) occuring when using newer versions of node, caused by https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2 Related: https://github.com/nodejs/node/issues/52554

Error: spawn EINVAL
    at ChildProcess.spawn (node:internal/child_process:421:11)
    at spawn (node:child_process:761:9)
    at ...\node_modules\@wasm-tool\wasm-pack-plugin\plugin.js:250:19
    at new Promise (<anonymous>)
    at runProcess (...\node_modules\@wasm-tool\wasm-pack-plugin\plugin.js:248:12)
    at spawnWasmPack (...\node_modules\@wasm-tool\wasm-pack-plugin\plugin.js:243:12)
    at ...\node_modules\@wasm-tool\wasm-pack-plugin\plugin.js:189:24 {
  errno: -4071,
  code: 'EINVAL',
  syscall: 'spawn'
}

travbid avatar May 25 '24 04:05 travbid

Would it be possible for this fix to be merged? Since node v20.12.2, this plugin can't launch npm on Windows, causing silent build breakage all over. It's unfortunate that a security fix for node broke backwards compatibility, but that's the way it appears to be.

magcius avatar Aug 01 '24 18:08 magcius