无法通过wmic启动npm

时间:2017-09-13 10:19:21

标签: windows batch-file npm wmic

如果我放入bat-file命令启动node

wmic process call create "npm run go","%~dp0."

一切正常:

D:\Temp\Supertemp\wmic npm>wmic process call create "node go","D:\Temp\Supertemp\wmic npm\."
Идет выполнение (Win32_Process)->Create()
Метод успешно вызван.
Параметры вывода:
instance of __PARAMETERS
{
        ProcessId = 13292;
        ReturnValue = 0;
};

但如果我使用npm代替

wmic process call create "npm run go","%~dp0."

发生错误9,according to msdn msdn表示找不到路径:

D:\Temp\Supertemp\wmic npm>wmic process call create "npm run go","D:\Temp\Supertemp\wmic npm\."
Идет выполнение (Win32_Process)->Create()
Метод успешно вызван.
Параметры вывода:
instance of __PARAMETERS
{
        ReturnValue = 9;
};

档案package.json

{
  "name": "wmic-npm-test",
  "version": "0.0.0",
  "scripts": {
    "go": "node go"
  }
}

档案go.js

setInterval(function () {
  console.log(require('process').cwd());
}, 1000);

如果只是从控制台启动npm,一切正常:

D:\Temp\Supertemp\wmic npm>npm run go

> wmic-npm-test@0.0.0 go D:\Temp\Supertemp\wmic npm
> node go

D:\Temp\Supertemp\wmic npm
D:\Temp\Supertemp\wmic npm

我是通过wmic to get process id开始的。

PS:Same question in Russian.

2 个答案:

答案 0 :(得分:1)

尝试这样(问题中我的评论中的模式详情):

@echo off

for %%a in (npm.cmd) do set "npm_path=%%~f$PATH:a"
echo %npm_path%

wmic process call create "cmd.exe /c call \"%npm_path%\" ","%~dp0."

答案 1 :(得分:1)

npm.cmd的文件npmsh文件:

D:\Temp\Supertemp\wmic npm>where npm
C:\Program Files\nodejs\npm
C:\Program Files\nodejs\npm.cmd

要执行npm命令,您需要指定要运行cmd-file:

wmic process call create "npm.cmd run go","%~dp0."