简体   繁体   English

从 PHP 文件调用“npm run generate”会导致“npm ERR! 代码生命周期”

[英]Calling “npm run generate” from PHP file results in “npm ERR! code ELIFECYCLE”

I'm calling npm run generate from PHP to generate static sites from NuxtJS.我正在调用npm run generate from PHP 以从 NuxtJS 生成 static 站点。 Running the below code results in Return Code: 254 - npm ERR!运行以下代码导致返回代码:254 - npm ERR! code 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);
?>

The generate.php file is in the same folder as all NuxtJS files. generate.php文件与所有 NuxtJS 文件位于同一文件夹中。 (https://example.com/12345/generate.php)

If I call npm run generate through SSH (via PuTTY) it works though.如果我调用npm run generate ,它可以工作。

What I'be tried so far:到目前为止我尝试了什么:

  • Update/reinstall all node_modules更新/重新安装所有 node_modules
  • Update/reinstall nodejs/npm更新/重新安装 nodejs/npm

How to fix this error?如何修复此错误?

Edit: Full error log:编辑:完整的错误日志:

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

You're overwriting PATH entirely rather than prepending to it, so npm scripts aren't able to run, because npm shells out.您正在完全覆盖PATH而不是在其前面添加,因此 npm 脚本无法运行,因为 npm 脱壳了。

EDIT:编辑:

In the chat, we discovered that PATH didn't exist at all in the PHP script, so rebuilt it manually: export PATH=/www/htdocs/w123456/nodejs/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin && npm run generate 2>&1" . Why PATH isn't accessible in the PHP script we don't know, but building it manually from the common paths works.在聊天中发现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 脚本中无法访问,但是从公共路径手动构建它是可行的。

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

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