安装 canvas 模块时出现 node-pre-gyp --fallback-to-build 错误

node-pre-gyp --fallback-to-build error while installing canvas module

我在将项目部署到 ElasticBeanStalk 时遇到问题,安装 canvas 2.7.0 模块时出现 node-pre-gyp --fallback-to-build 错误。

我不确定这是不是权限错误

 Error: EACCES: permission denied, mkdir '/var/app/staging/node_modules/@tensorflow/tfjs-node/deps'

这里是我在 eb-engine.log

中的错误
> canvas@2.6.1 install /var/app/staging/node_modules/canvas
> node-pre-gyp install --fallback-to-build

Failed to execute '/opt/elasticbeanstalk/node-install/node-v14.16.0-linux-x64/bin/node /opt/elasticbeanstalk/node-install/node-v14.16.0-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/var/app/staging/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/var/app/staging/node_modules/canvas/build/Release --napi_version=7 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v83' (1)

2021/04/29 17:18:19.518886 [ERROR] An error occurred during execution of command [app-deploy] - [Use NPM to install dependencies]. Stop running the command. Error: Command /bin/sh -c npm --production install failed with error exit status 1. Stderr:(node:3446) UnhandledPromiseRejectionWarning: Error: EACCES: permission denied, mkdir '/var/app/staging/node_modules/@tensorflow/tfjs-node/deps'
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3446) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:3446) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
node-pre-gyp WARN Using needle for node-pre-gyp https download 
node-pre-gyp WARN Pre-built binaries not installable for canvas@2.6.1 and node@14.16.0 (node-v83 ABI, glibc) (falling back to source compile with node-gyp) 
node-pre-gyp WARN Hit error EACCES: permission denied, mkdir '/var/app/staging/node_modules/canvas/build' 
gyp WARN EACCES current user ("healthd") does not have permission to access the dev dir "/root/.cache/node-gyp/14.16.0"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/var/app/staging/node_modules/canvas/.node-gyp"
gyp WARN install got an error, rolling back install
gyp WARN install got an error, rolling back install
gyp ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/var/app/staging/node_modules/canvas/.node-gyp'
gyp ERR! System Linux 4.14.225-169.362.amzn2.x86_64
gyp ERR! command "/opt/elasticbeanstalk/node-install/node-v14.16.0-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v14.16.0-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/var/app/staging/node_modules/canvas/build/Release/canvas.node" "--module_name=canvas" "--module_path=/var/app/staging/node_modules/canvas/build/Release" "--napi_version=7" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v83"
gyp ERR! cwd /var/app/staging/node_modules/canvas
gyp ERR! node -v v14.16.0
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok 
node-pre-gyp ERR! build error 
node-pre-gyp ERR! stack Error: Failed to execute '/opt/elasticbeanstalk/node-install/node-v14.16.0-linux-x64/bin/node /opt/elasticbeanstalk/node-install/node-v14.16.0-linux-x64/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/var/app/staging/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/var/app/staging/node_modules/canvas/build/Release --napi_version=7 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v83' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/var/app/staging/node_modules/canvas/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:315:20)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:1048:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5)
node-pre-gyp ERR! System Linux 4.14.225-169.362.amzn2.x86_64
node-pre-gyp ERR! command "/opt/elasticbeanstalk/node-install/node-v14.16.0-linux-x64/bin/node" "/var/app/staging/node_modules/canvas/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /var/app/staging/node_modules/canvas
node-pre-gyp ERR! node -v v14.16.0
node-pre-gyp ERR! node-pre-gyp -v v0.11.0
node-pre-gyp ERR! not ok 
npm WARN only4laugh@1.0.0 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! canvas@2.6.1 install: `node-pre-gyp install --fallback-to-build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the canvas@2.6.1 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我也在我的 package.json 中提到了节点和 npm 版本,并且也添加到 .npmrc 文件中

"engines": {
    "node": ">=8.12.0",
    "npm": ">=6.0.0"
  }

我知道这是权限错误,当服务器端 npm install 命令运行时,ec2 实例没有创建任何文件夹的权限,所以我找到了 2 个解决方案,第一个是创建 .npmrc 文件并设置不安全- perm=true,还有一个是创建.ebextension文件设置root权限