Skip to content

Error: spawn EINVAL on Windows #715

Open

Description

@samuelmaddock

The latest versions of Node include a security vulnerability fix which now requires calling spawn() with shell: true on Windows (Node security release blog).

node-pre-gyp info using node-pre-gyp@1.0.11
node-pre-gyp info using node@20.13.0 | win32 | x64
node-pre-gyp ERR! UNCAUGHT EXCEPTION 
node-pre-gyp ERR! stack Error: spawn EINVAL
node-pre-gyp ERR! stack     at ChildProcess.spawn (node:internal/child_process:421:11)
node-pre-gyp ERR! stack     at Object.spawn (node:child_process:761:9)
node-pre-gyp ERR! stack     at module.exports.run_gyp (C:\Users\circleci\project\node_modules\@mapbox\node-pre-gyp\lib\util\compile.js:80:18)
node-pre-gyp ERR! stack     at C:\Users\circleci\project\node_modules\@mapbox\node-pre-gyp\lib\configure.js:44:15
node-pre-gyp ERR! stack     at handle_gyp_opts (C:\Users\circleci\project\node_modules\@mapbox\node-pre-gyp\lib\util\handle_gyp_opts.js:101:10)
node-pre-gyp ERR! stack     at configure (C:\Users\circleci\project\node_modules\@mapbox\node-pre-gyp\lib\configure.js:12:3)
node-pre-gyp ERR! stack     at self.commands.<computed> [as configure] (C:\Users\circleci\project\node_modules\@mapbox\node-pre-gyp\lib\node-pre-gyp.js:86:37)
node-pre-gyp ERR! stack     at run (C:\Users\circleci\project\node_modules\@mapbox\node-pre-gyp\lib\main.js:81:30)
node-pre-gyp ERR! stack     at Object.<anonymous> (C:\Users\circleci\project\node_modules\@mapbox\node-pre-gyp\lib\main.js:125:1)
node-pre-gyp ERR! stack     at Module._compile (node:internal/modules/cjs/loader:1358:14)

I'm currently working around this by using patch-package with the following patch:

diff --git a/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js b/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js
index 956e5aa..0051fce 100644
--- a/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js
+++ b/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js
@@ -77,7 +77,9 @@ module.exports.run_gyp = function(args, opts, callback) {
     }
   }
   const final_args = cmd_args.concat(args);
-  const cmd = cp.spawn(shell_cmd, final_args, { cwd: undefined, env: process.env, stdio: [0, 1, 2] });
+  // Add 'shell' on Windows due to security vulnerability
+  // https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2
+  const cmd = cp.spawn(shell_cmd, final_args, { cwd: undefined, env: process.env, stdio: [0, 1, 2], shell: process.platform === 'win32' });
   cmd.on('error', (err) => {
     if (err) {
       return callback(new Error("Failed to execute '" + shell_cmd + ' ' + final_args.join(' ') + "' (" + err + ')'));

Activity

  1. Stanzilla commented on Jan 13, 2025

    @Stanzilla

    @benmccann any chance this could be picked up for 2.0?

  2. cclauss commented on Jan 13, 2025

    @cclauss
    Collaborator

    Does this happen on v2.0.0-rc.0? https://github.com/mapbox/node-pre-gyp/tags

  3. benmccann commented on Jan 13, 2025

    @benmccann
    Collaborator

    Should we just always set shell: true? I'm afraid I don't know the implications of doing so. I see the patch in the issue description does it only on Windows, but I'm wary about creating an inconsistency between platforms

  4. Stanzilla commented on Jan 13, 2025

    @Stanzilla

    Should we just always set shell: true? I'm afraid I don't know the implications of doing so. I see the patch in the issue description does it only on Windows, but I'm wary about creating an inconsistency between platforms

    The change in node was specifically just for Windows and bat/cmd files so imo doing it just for Windows, like it was proposed, is fine

  5. benmccann commented on Jan 13, 2025

    @benmccann
    Collaborator

    The proposal does it for non-bat/cmd files in Windows though. If we're going to do it as minimally as possible perhaps we should check for that too

  6. Stanzilla commented on Jan 22, 2025

    @Stanzilla

    Does this happen on v2.0.0-rc.0? https://github.com/mapbox/node-pre-gyp/tags

    I don't have a Windows system with access to the codebase where this happens, sorry

  7. polar2031 commented on Jan 23, 2025

    @polar2031

    The patch saves my day, but it will not work when node is installed in path containing space like Program File
    https://nodejs.org/api/child_process.html#spawning-bat-and-cmd-files-on-windows
    Simply make shell_cmd quoted should fix it.

  8. benmccann commented on Feb 6, 2025

    @benmccann
    Collaborator

    It seems when using spawn: true you need to escape the command and it's arguments. Some details here: https://www.npmjs.com/package/cross-spawn

  9. Stanzilla commented on Feb 6, 2025

    @Stanzilla

    So one could just always escape the commands and arguments yet only set shell to true as in the patch suggested above, no?

  10. Stanzilla commented on Apr 15, 2025

    @Stanzilla

    @benmccann 🙏 anything I can do to help move this forward?

  11. benmccann commented on Apr 15, 2025

    @benmccann
    Collaborator

    Perhaps we should just use cross-spawn. I'm not sure I want to try rebuilding the logic they already have

  12. ayan4m1 commented on Jul 27, 2025

    @ayan4m1

    Any update on this? Currently blocking my ability to build/use tensorflow.js. Tried with Node 20, 22, and 24.

    # This file contains the result of Yarn building a package (@tensorflow/tfjs-node@npm:4.22.0)
    # Script name: install
    
    CPU-windows-4.22.0.zip
    * Building TensorFlow Node.js bindings
    node-pre-gyp install failed with error: Error: Command failed: node-pre-gyp install --fallback-to-build
    node-pre-gyp info it worked if it ends with ok
    node-pre-gyp info using node-pre-gyp@1.0.9
    node-pre-gyp info using node@22.12.0 | win32 | x64
    (node:21280) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
    (Use `node --trace-deprecation ...` to show where the warning was created)
    node-pre-gyp info check checked for "C:\code\fantasy-card-art-loras\node_modules\@tensorflow\tfjs-node\lib\napi-v8\tfjs_binding.node" (not found)
    node-pre-gyp http GET https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/4.22.0/CPU-windows-4.22.0.zip
    node-pre-gyp ERR! install response status 404 Not Found on https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/4.22.0/CPU-windows-4.22.0.zip 
    node-pre-gyp WARN Pre-built binaries not installable for @tensorflow/tfjs-node@4.22.0 and node@22.12.0 (node-v127 ABI, unknown) (falling back to source compile with node-gyp) 
    node-pre-gyp WARN Hit error response status 404 Not Found on https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/4.22.0/CPU-windows-4.22.0.zip 
    node-pre-gyp ERR! UNCAUGHT EXCEPTION 
    node-pre-gyp ERR! stack Error: spawn EINVAL
    node-pre-gyp ERR! stack     at ChildProcess.spawn (node:internal/child_process:420:11)
    node-pre-gyp ERR! stack     at Object.spawn (node:child_process:753:9)
    node-pre-gyp ERR! stack     at module.exports.run_gyp (C:\code\fantasy-card-art-loras\node_modules\@mapbox\node-pre-gyp\lib\util\compile.js:80:18)
    node-pre-gyp ERR! stack     at build (C:\code\fantasy-card-art-loras\node_modules\@mapbox\node-pre-gyp\lib\build.js:41:13)
    node-pre-gyp ERR! stack     at self.commands.<computed> [as build] (C:\code\fantasy-card-art-loras\node_modules\@mapbox\node-pre-gyp\lib\node-pre-gyp.js:86:37)
    node-pre-gyp ERR! stack     at run (C:\code\fantasy-card-art-loras\node_modules\@mapbox\node-pre-gyp\lib\main.js:81:30)
    node-pre-gyp ERR! stack     at process.processTicksAndRejections (node:internal/process/task_queues:85:11)
    node-pre-gyp ERR! System Windows_NT 10.0.26100
    node-pre-gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\code\\fantasy-card-art-loras\\node_modules\\@mapbox\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build"
    node-pre-gyp ERR! cwd C:\code\fantasy-card-art-loras\node_modules\@tensorflow\tfjs-node
    node-pre-gyp ERR! node -v v22.12.0
    node-pre-gyp ERR! node-pre-gyp -v v1.0.9
    

    Running npm rebuild @tensorflow/tfjs-node --build-addon-from-source completes successfully and then I can install @tensorflow/tfjs-node via yarn with no errors, but I get

    node:internal/modules/cjs/loader:1734
      return process.dlopen(module, path.toNamespacedPath(filename));
                     ^
    
    Error: The specified module could not be found.
    \\?\C:\code\fantasy-card-art-loras\node_modules\@tensorflow\tfjs-node\lib\napi-v8\tfjs_binding.node
        at Object..node (node:internal/modules/cjs/loader:1734:18)
        at Module.load (node:internal/modules/cjs/loader:1318:32)
        at Function._load (node:internal/modules/cjs/loader:1128:12)
        at TracingChannel.traceSync (node:diagnostics_channel:322:14)
        at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
        at Module.require (node:internal/modules/cjs/loader:1340:12)
        at require (node:internal/modules/helpers:138:16)
        at Object.<anonymous> (C:\code\fantasy-card-art-loras\node_modules\@tensorflow\tfjs-node\dist\index.js:72:16)
        at Module._compile (node:internal/modules/cjs/loader:1565:14)
        at Object..js (node:internal/modules/cjs/loader:1708:10) {
      code: 'ERR_DLOPEN_FAILED'
    }
    
    Node.js v22.12.0
    

    but that .node file exists...

  13. chrisgh1 commented on Aug 13, 2025

    @chrisgh1

    Just wanted to chime in and say that we are having the same issue with node-pre-gyp 2.0.0. Discovered this issue when trying to install github.com/IBM/node-odbc/

    Currently working around this by using the patch-package referenced above by samuelmaddock

  14. removed their assignment
    on Jan 10, 2026
  15. Stanzilla commented on Mar 9, 2026

    @Stanzilla

    @cclauss can this be looked into, please? It's been almost two years now.

  16. cclauss commented on Mar 9, 2026

    @cclauss
    Collaborator

    I have never owned, and I do not have access to a Windows box. Would it be possible to create a GitHub Action that raises this error?

  17. Stanzilla commented on Mar 9, 2026

    @Stanzilla

    I have never owned, and I do not have access to a Windows box. Would it be possible to create a GitHub Action that raises this error?

    Hrm possibly, would you guys be open to accepting that as a contribution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions