Using nodejs's spawn causes “unknown option — ” and “[Error: spawn ENOENT]” errors

后端 未结 3 2114
北荒
北荒 2020-11-27 06:03

I\'m trying to get spawn to effect an rm -rf node_modules followed by npm install (on windows 7; nx commands courtesy of a tr

相关标签:
3条回答
  • 2020-11-27 06:38

    I think this may be some sort of cygwin gotcha. I'm running Ubuntu 12.04 and tried to duplicate your problem, but it works just fine for me. In short, I don't see anything you are doing wrong.

    If it is complaining about the option, maybe split it up into multiple options like so:

    child = spawn("rm", ["-r", "-f", "node_modules"]);
    

    That's kind of a hail mary, but that works on my Ubuntu 12.04 as well. You might try to just delete a single file and see if you get the same thing.

    child = spawn("rm", ["/home/username/Desktop/TestFile"]);
    

    If that still fails, then you know you are working against some crazy stuff.

    You could even try to just execute a command with no parameters like so:

    child = spawn("ls");
    

    If that still fails, you aren't likely to get spawn to work at all would be my guess and be grateful that at least exec is working.

    Not much in the realm of answers for you, but like I said, I can't see anything you are doing incorrectly.

    Furthermore, I don't see how your npm command is going to work because you aren't specifying what to install, but that being said, it fails in a different way than I'm seeing it fail here if I use the same command. . . I see lots of stderr output, not an overall error.

    BTW, I'm running node v0.8.21. You can query that by node -v. If you are running another version, maybe give 0.8.21 a try.

    0 讨论(0)
  • 2020-11-27 06:45

    Use full path for the process, like:

    var cmd = require('child_process').spawn("C:\\windows\\system32\\cmd.exe");
    
    0 讨论(0)
  • 2020-11-27 06:56

    After lots of trying different things, I finally had a look at what "npm" actually is on windows, and it turns out to be a bash script called npm, as well as a windows-native batch script called npm.cmd (no idea why it's .cmd, that should be .bat, but there you have it). Windows's command resolver will see npm, notice that it's not an executable, see npm.cmd, and then notice that IS an executable, and will then use that instead. This is helpful when you're in a terminal, but spawn() will not do any such resolution: passing it npm will make it fail because it's not an executable. Passing it npm.cmd as command, however, works just fine.

    (Also, not sure why rm was failing earlier, since that actually works correctly without any changes that I can tell. Probably misread that as part of the problem when in fact it wasn't.)

    So: if you run into spawn saying ENOENT in windows, when the command you're trying to trigger works in a plain command prompt, find out if the command you're calling is a true executable, or whether there's a .bat/.cmd file that the command prompt will "helpfully" run for you instead. If so, spawn that.

    edit

    since this post is still getting upvotes, a good way to ensure the command always works is to bootstrap it based on process.platform, which will be win32 for windows.

    var npm = (process.platform === "win32" ? "npm.cmd" : "npm"),
        child = spawn(npm, ["install", ...]);
    ...
    

    edit specific to the use-case that triggered this error

    since posting this question (and its answer), several packages have been released that allow running npm tasks without having to rely on exec or spawn, and you should use them instead.

    Probably the most popular is npm-run-all which doesn't just give you the power to run any npm task from other npm scripts as well as from Node, but also adds commands to run multiple npm scripts in series or in parallel, with or without wildcards.

    In the context of the original question, where the error got thrown because I was trying to run npm as an exec/spawn in order to effect a cleanup and reinstall, the modern solution is to have a dedicated cleaning task in package.json:

    {
      ...
      "scripts": {
        "clean": "rimraf ./node_modules",
        ...
      },
      ...
    }
    

    And to then invoke that clean task followed by the install command on the command line as

    > npm run clean && npm install
    

    Or, from inside some Node script, using:

    const runAll = require("npm-run-all");
    ...
    runAll(["clean", "install"])
        .then(() => {
            console.log("done!");
        })
        .catch(err => {
            console.log("failed!");
        });
    
    0 讨论(0)
提交回复
热议问题