从 PHP 文件调用 "npm run generate" 导致 "npm ERR! code ELIFECYCLE"

Calling "npm run generate" from PHP file results in "npm ERR! code ELIFECYCLE"

我正在从 PHP 调用 npm run generate 以从 NuxtJS 生成静态站点。 运行 下面的代码导致 Return 代码:254 - npm ERR!代码 ELIFECYCLE.

<?php
exec("export PATH=/www/htdocs/w01234567/nodejs/bin && npm run generate 2>&1", $out, $result);
echo "ReturnCode: " .$result ."<br>";
echo "Output: " ."<br>";
echo "<pre>"; print_r($out);
?>

generate.php 文件与所有 NuxtJS 文件位于同一文件夹中。 (https://example.com/12345/generate.php)

如果我通过 SSH(通过 PuTTY)调用 npm run generate,它仍然有效。

到目前为止我尝试过的:

如何解决这个错误?

编辑:完整错误日志:

0 info it worked if it ends with ok
1 verbose cli [ 'node', '/www/htdocs/w12345678/nodejs/bin/npm', 'run', 'generate' ]
2 info using npm@6.14.10
3 info using node@v15.4.0
4 verbose node symlink /www/htdocs/w12345678/nodejs/bin/node
5 verbose run-script [ 'pregenerate', 'generate', 'postgenerate' ]
6 info lifecycle new-blog@1.0.0~pregenerate: new-blog@1.0.0
7 info lifecycle new-blog@1.0.0~generate: new-blog@1.0.0
8 verbose lifecycle new-blog@1.0.0~generate: unsafe-perm in lifecycle true
9 verbose lifecycle new-blog@1.0.0~generate: PATH: /www/htdocs/w12345678/nodejs/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/www/htdocs/w12345678/example.com/abcdefgh123456789/node_modules/.bin:/www/htdocs/w12345678/nodejs/bin
10 verbose lifecycle new-blog@1.0.0~generate: CWD: /www/htdocs/w12345678/example.com/abcdefgh123456789
11 silly lifecycle new-blog@1.0.0~generate: Args: [ '-c', 'nuxt generate' ]
12 info lifecycle new-blog@1.0.0~generate: Failed to exec generate script
13 silly lifecycle new-blog@1.0.0~generate: Returned: code: -2  signal: null
14 info lifecycle new-blog@1.0.0~generate: Failed to exec generate script
15 verbose stack Error: new-blog@1.0.0 generate: `nuxt generate`
15 verbose stack spawn sh ENOENT
15 verbose stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:276:19)
15 verbose stack     at onErrorNT (node:internal/child_process:476:16)
15 verbose stack     at processTicksAndRejections (node:internal/process/task_queues:80:21)
16 verbose pkgid new-blog@1.0.0
17 verbose cwd /www/htdocs/w12345678/example.com/abcdefgh123456789
18 verbose Linux 4.15.0-129-generic
19 verbose argv "node" "/www/htdocs/w12345678/nodejs/bin/npm" "run" "generate"
20 verbose node v15.4.0
21 verbose npm  v6.14.10
22 error code ELIFECYCLE
23 error syscall spawn sh
24 error file sh
25 error path sh
26 error errno -2
27 error new-blog@1.0.0 generate: `nuxt generate`
27 error spawn sh ENOENT
28 error Failed at the new-blog@1.0.0 generate script.
28 error This is probably not a problem with npm. There is likely additional logging output above.
29 verbose exit [ -2, true ]
-export PATH=/www/htdocs/w01234567/nodejs/bin
+export PATH=/www/htdocs/w01234567/nodejs/bin:$PATH

您正在完全覆盖 PATH 而不是在其前面添加,因此 npm 脚本无法 运行,因为 npm 会退出。

编辑:

在聊天中,我们发现PHP脚本中根本不存在PATH,于是手动重新构建:export PATH=/www/htdocs/w123456/nodejs/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin && npm run generate 2>&1"。为什么 PATH 在我们不知道的 PHP 脚本中不可访问,但是从通用路径手动构建它是可行的。