简体   繁体   English

NodeJS:运行dot slash命令行Windows

[英]NodeJS: Run dot slash command line Windows

I'm trying to get the following code which should execute shell command to wor in OSX and Windows. 我正在尝试获取以下代码,这些代码应该在OSX和Windows中执行shell命令。

const exec = require("child_process").exec;
const runCommand = (cmd) => exec(cmd,
    function (error, stdout, stderr) {
        if (stdout.length > 0) {
            console.log(stdout);
        }
    });

For the following runCommand("./node_modules/.bin/someBinary") it does not work in Windows. 对于以下runCommand("./node_modules/.bin/someBinary")它在Windows中不起作用。 But it works fine in OSX. 但它在OSX中运行良好。 So I wonder what modifications do I need for it to run in Windows as well? 所以我想知道在Windows中运行它需要哪些修改?

Have you considered giving a try at npm-exec ? 您是否考虑过试试npm-exec

Loads the same environment that would be present for npm run-script , but executes arbitrary bash command lines. 加载与npm run-script相同的环境,但执行任意bash命令行。 (even on windows! Thanks to bashful). (甚至在窗户上!感谢害羞)。 This includes modifying $PATH so scripts in node_modules/.bin will be used before global modules. 这包括修改$ PATH,以便在全局模块之前使用node_modules / .bin中的脚本。

So you could npm-exec someBinary instead of using a relative path breaking Windows / OSX compatibility 所以你可以使用npm-exec someBinary而不是使用破坏Windows / OSX兼容性的相对路径

If this module does not suit you, you can also give a try at npm bin as explained in this answer . 如果这个模块不适合你,你也可以试试npm bin本回答所述

Okay, so in the end I solved it with checking which platform I'm in and in turn I just wrote the same command with OS specific so it would run. 好的,所以最后我通过检查我所在的平台来解决它,反过来我只是用特定的OS编写了相同的命令,所以它会运行。

In short I used process.platform to find out which OS it is. 简而言之,我使用process.platform来找出它是哪个操作系统。 Not the most elegant. 不是最优雅的。 But at least it solved my problem. 但至少它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM