简体   繁体   English

运行 NPM 子进程导致“找不到模块”

[英]Running NPM subprocess results in "Cannot find module"

I'm creating a python script that should run my backend and frontend using npm run start as both are node projects.我正在创建一个 python 脚本,它应该使用npm run start运行我的后端和前端,因为它们都是节点项目。 The subprocess should not wait until they finish to execute the next line of code, I would like to detach them.子进程不应等到它们完成执行下一行代码,我想将它们分离。

I'm developing on Windows 10.我正在 Windows 10 上开发。

When doing so I get the following error:这样做时,我收到以下错误:

Error: Cannot find module 'C:\CodigoAsisa\Backend\npm'
←[90m    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:667:27)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)←[39m
←[90m    at internal/main/run_main_module.js:17:47←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m,
  requireStack: []
}
internal/modules/cjs/loader.js:818
  throw err;

I'm using the following code:我正在使用以下代码:

subprocess.Popen(["node", "npm", "run", "start"], cwd="C:\\CodigoAsisa\\Backend",
                       stdout=subprocess.PIPE,
                       universal_newlines=True)

I've tried also the following code:我也尝试过以下代码:

subprocess.run('cd C:\\CodigoAsisa\\Frontend && npm run start',shell=True)

When using the above (the one using subprocess.run() ) code I get no errors related to node (nor errors whatsoever), but this doesn't allow me to detach my process, thus continuing to the next line of code.使用上述代码(使用subprocess.run()的代码)时,我没有收到与节点相关的错误(也没有任何错误),但这不允许我分离我的进程,因此继续下一行代码。 I need this process to run in the background, and, as far as I can tell, subprocess.run() waits for the process to end.我需要这个进程在后台运行,据我所知, subprocess.run() 等待进程结束。

Any help is appreciated!任何帮助表示赞赏!

I solved this by using where npm which returned my executable location, in this case located at C:\Program Files\nodejs\npm.cmd Subsequently changing my code to:我通过使用where npm解决了这个问题,它返回了我的可执行位置,在这种情况下位于 C:\Program Files\nodejs\npm.cmd 随后将我的代码更改为:

subprocess.Popen(["C:\\Program Files\\nodejs\\npm.cmd", "run","start"],cwd="C:\\CodigoAsisa\\Frontend",
                           stdout=devnull,
                           universal_newlines=True)

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

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