将bcrypt添加到package.json时如何用docker节点alpine Image解决"Could not find any Python installation to use"?

How to solve "Could not find any Python installation to use" with docker node alpine Image when adding bcrypt to package.json?

在我将 bcrypt 添加到我的 package.json 之前,一切正常。现在,我收到以下错误消息。

这是我的摘录 package.json:

  "dependencies": {
    "bcrypt": "3.0.6",
    "express": "^4.17.1",
    "mongodb": "^3.3.1",
    "nodemailer": "^6.3.0",
    "pm2": "^3.5.1",
    "redis": "^2.8.0",
    "xlsx": "^0.15.0"
  },

这是我的 docker 文件。我正在使用官方节点高山图像。请问alpine有没有安装phyton

FROM node:13.5.0-alpine

WORKDIR /usr/app

COPY ./src .

RUN npm install

我在 运行 docker-compose:

时收到此错误消息
node-pre-gyp WARN Using request for node-pre-gyp https download
node-pre-gyp WARN Tried to download(404): https://github.com/kelektiv/node.bcrypt.js/releases/download/v3.0.6/bcrypt_lib-v3.0.6-node-v79-linux-x64-musl.tar.gz
node-pre-gyp WARN Pre-built binaries not found for bcrypt@3.0.6 and node@13.5.0 (node-v79 ABI, musl) (falling back to source compile with node-gyp)
gyp ERR! find Python
gyp ERR! find Python Python is not set from command line or npm configuration
gyp ERR! find Python Python is not set from environment variable PYTHON
gyp ERR! find Python checking if "python" can be used
gyp ERR! find Python - "python" is not in PATH or produced an error
gyp ERR! find Python checking if "python2" can be used
gyp ERR! find Python - "python2" is not in PATH or produced an error
gyp ERR! find Python checking if "python3" can be used
gyp ERR! find Python - "python3" is not in PATH or produced an error
gyp ERR! find Python
gyp ERR! find Python **********************************************************
gyp ERR! find Python You need to install the latest version of Python.
gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
gyp ERR! find Python you can try one of the following options:
gyp ERR! find Python - Use the switch --python="/path/to/pythonexecutable"
gyp ERR! find Python   (accepted by both node-gyp and npm)
gyp ERR! find Python - Set the environment variable PYTHON
gyp ERR! find Python - Set the npm configuration variable python:
gyp ERR! find Python   npm config set python "/path/to/pythonexecutable"
gyp ERR! find Python For more information consult the documentation at:
gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
gyp ERR! find Python **********************************************************
gyp ERR! find Python

gyp ERR! configure error
gyp ERR! stack Error: Could not find any Python installation to use
gyp ERR! stack     at PythonFinder.fail (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:307:47)
gyp ERR! stack     at PythonFinder.runChecks (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:136:21)
gyp ERR! stack     at PythonFinder.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:179:16)
gyp ERR! stack     at PythonFinder.execFileCallback (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/find-python.js:271:16)
gyp ERR! stack     at exithandler (child_process.js:309:5)
gyp ERR! stack     at ChildProcess.errorhandler (child_process.js:321:5)
gyp ERR! stack     at ChildProcess.emit (events.js:305:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12)
gyp ERR! stack     at onErrorNT (internal/child_process.js:469:16)
gyp ERR! stack     at processTicksAndRejections (internal/process/task_queues.js:84:21)
gyp ERR! System Linux 4.15.0-47-generic
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/usr/app/node_modules/bcrypt/lib/binding/bcrypt_lib.node" "--module_name=bcrypt_lib" "--module_path=/usr/app/node_modules/bcrypt/lib/binding" "--napi_version=5" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v79"
gyp ERR! cwd /usr/app/node_modules/bcrypt
gyp ERR! node -v v13.5.0
gyp ERR! node-gyp -v v5.0.5
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/app/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/usr/app/node_modules/bcrypt/lib/binding --napi_version=5 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v79' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/usr/app/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:305:20)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:1028:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
node-pre-gyp ERR! System Linux 4.15.0-47-generic
node-pre-gyp ERR! command "/usr/local/bin/node" "/usr/app/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /usr/app/node_modules/bcrypt
node-pre-gyp ERR! node -v v13.5.0
node-pre-gyp ERR! node-pre-gyp -v v0.12.0
node-pre-gyp ERR! not ok
Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/app/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/usr/app/node_modules/bcrypt/lib/binding --napi_version=5 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v79' (1)
npm WARN testapp@0.0.1 No repository field.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.11 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.11: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

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

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2019-12-24T14_22_05_060Z-debug.log
ERROR: Service 'web' failed to build: The command '/bin/sh -c npm install' returned a non-zero code: 1

我该如何解决这个问题?我必须在 alpine 上安装 phyton 还是只添加 env 变量?如果是这样,请告诉我如何。我已经搜索了解决方案,但没有人工作...

Linux Alpine 是非常简约的图像。如果性能不是限制,您可能需要考虑使用 官方节点图像 ,它具有您需要的所有依赖项,只需担心开发:https://github.com/nodejs/docker-node 所以,你可以有一个 Dockerfile 这样的:

FROM node:10
WORKDIR /usr/app
COPY ./src .
RUN npm install
EXPOSE 3000 # Use here the port you want to expose

在 npm 安装之前将此添加到 docker 文件中:

RUN apk add --update python make g++\
   && rm -rf /var/cache/apk/*

如果您正在使用 Python 3,请记住使用 python3 而不是 python.

为了完整起见:我解决了。

解决方案是将依赖关系 "bcrypt" 替换为 "bcryptjs"。

使用 bcryptjs 没有错误。无论是标准节点 docker 图像还是节点高山 docker 图像。

以管理员权限打开 Powershell 并运行执行此命令,然后继续安装 bcrypt

Windows: npm install --global --production windows-build-tools

我已经通过从 python 下载并安装解决了这个问题 https://www.python.org/downloads/windows/

我的问题是 pg-native 依赖错误地使用了 node-gyp。

从这个 link - node-gyp failure pg-native

中找到了解决方案

摘自有帮助的评论:

A: Don't use two different db libraries by accident: i.e. pg and pg.js.

B: Be cautious using things like sequelize that you may have flagged the 'native' option in initializing your connection to the DB. This will also often cause this error.

C: Lastly, I often remove the pg-native library entirely. Causes strange errors that are not consistent with node-gyp.

在 Dockerfile

中将文件组合到 运行 节点服务器

FROM node:16.8.0-alpine
ENV SERVER_HOME=/usr/src/server/
WORKDIR $SERVER_HOME
COPY ./package*.json $SERVER_HOME
RUN npm install -g npm@7.24.0
RUN apk add --update python3 make g++ && rm -rf /var/cache/apk/*
RUN npm install
COPY . $SERVER_HOME
#EXPOSE 5000
CMD npm run build
CMD npm run start

我在 alpine 映像中安装节点模块时遇到问题,因此在 alpine 中找不到 python,所以我 运行 使用 python3 运行 apk add --update python3 make g++ && rm -rf /var/cache/apk/* 如果您仍然遇到问题,一切正常,请将 npm install 替换为 npm 安装 --force

Alpine 的正确解决方案是

RUN apk add --no-cache python3 make g++