Home > Software design >  Running NPM subprocess results in "Cannot find module"
Running NPM subprocess results in "Cannot find module"

Time:12-24

I'm creating a python script that should run my backend and frontend using npm run start as both are node projects. 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.

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. I need this process to run in the background, and, as far as I can tell, subprocess.run() waits for the process to end.

Any help is appreciated!

CodePudding user response:

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:

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