使用 spawn node.js 方法执行 bash 脚本 returns 错误

Executing bash script with spawn node.js method returns errors

我找不到使用 spawn 方法执行节点命令的方法。我的脚本是:

node node_modules/.bin/webpack-dev-server --port 8081 --content-base app

我试着像这样执行它:

spawn('node', ['node_modules/.bin/webpack-dev-server --port 8081 --content-base app']);

这个returns一个错误

Cannot find module '/Users/myuser/code/gui/node_modules/.bin/webpack-dev-server --port 8081 --content-base app'

我也试过这个:

spawn('node', ['node_modules/.bin/webpack-dev-server',' --port 8081 ', '--content-base app']);

它运行我的 webpack 服务器,但它没有考虑 portcontent-base 参数。对于这种情况,错误是:

ERROR in Entry module not found: Error: Cannot resolve module ' --port 8081 '

有什么想法吗?谢谢!

哦,我忘了补充一点,脚本在 exec 方法下运行良好。

你在第二个例子中的第二个参数中有多余的空格。试试这个:

const args = [
  'node_modules/.bin/webpack-dev-server',
  '--port',
  '8081',
  '--content-base',
  'app'
];
spawn('node', args);