我开发了一个工具,它生成一个 scss 文件,运行 npm run prod 并将生成的 app.css 上传到服务器。
在本地这没有问题。我只需运行 shell_exec('npm run production')
即可成功运行。
现在,今天,我将该工具推出到由 Laravel Forge 管理的 DigitalOcean 服务器。现在,NPM 命令的执行不再有效。相反,我在运行 npm run production
时看到此错误:
ERR! file sh
npm ERR! path sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm
当我现在将脚本更改为 npm -v
时,我看到:
6.9.0
有人知道如何让 NPM 在这里运行吗?
(生产是此命令的别名:cross-env NODE_ENV=生产 node_modules/webpack/bin/webpack.js --no-progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js
)
更新:
现在,我尝试使用 yarn 。运行yarn install
工作正常。结果是预料之中的。但是运行yarn rundevelopment会出现以下错误:
2020-11-07 20:03:55 STDOUT: yarn run v1.17.3
2020-11-07 20:03:55 STDOUT: $ npm run development
2020-11-07 20:03:55 STDERR: /bin/sh: 1:
2020-11-07 20:03:55 STDERR: npm: not found
2020-11-07 20:03:55 STDERR: error Command failed with exit code 127.
2020-11-07 20:03:55 STDOUT: info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
如果我通过 ssh 在完全相同的目录中手动运行 yarn 运行开发,则一切正常。
更新 2: 我想,也许问题是,Laravel 是由另一个用户在 SSH 中执行的,而不是我。但两者都是用户伪造。
最佳答案
我找到了两种可能的解决方案。
其中一个是几年前关于 shell_exec
和 npm
的问题,解决方案是 $PATH
不包含 /bin
当作为 npm 脚本调用时,因此无法根据 错误文件 sh
找到 /bin/sh
(不是我的话,请参阅 the original question )。他们的解决方案是将 /bin/sh
符号链接(symbolic link)到 /usr/bin/sh
:
ln -s /bin/sh /usr/bin/sh
另一个解决方案可能是使用 Symony 的 Process
:
use Symfony\Component\Process\Process;
(new Process(['npm', 'run', 'production'], base_path()))
->setTimeout(null)
->run(function ($type, $output) {
$this->output->write($output);
});
这两种解决方案都没有经过测试,因为我目前无法访问 DigitalOcean 服务器或 Laravel Forge 帐户。
关于node.js - 从 Laravel Forge-Setup 中的 Laravel 应用程序执行 "npm",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64731224/