问题描述
项目在本地跑的好好地,使用Windows电脑和MAC电脑,重新下载依赖运行项目均无异常。
使用docker部署项目,遇到如下报错
[2023-02-22 09:55:13.784] debug: ⛔️ Server wasn't able to start properly.
Wed, Feb 22 2023 5:55:13 pm[2023-02-22 09:55:13.786] error: Could not load js config file /opt/app/node_modules/@strapi/plugin-upload/strapi-server.js:
Wed, Feb 22 2023 5:55:13 pmSomething went wrong installing the "sharp" module
Wed, Feb 22 2023 5:55:13 pm
Wed, Feb 22 2023 5:55:13 pmCannot find module '../build/Release/sharp-linuxmusl-x64.node'
Wed, Feb 22 2023 5:55:13 pmRequire stack:
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/node_modules/sharp/lib/sharp.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/node_modules/sharp/lib/constructor.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/node_modules/sharp/lib/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/server/services/image-manipulation.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/server/services/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/server/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/strapi-server.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/utils/lib/import-default.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/utils/lib/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/strapi/lib/Strapi.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/strapi/lib/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/strapi/lib/commands/start.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/strapi/bin/strapi.js
Wed, Feb 22 2023 5:55:13 pm
Wed, Feb 22 2023 5:55:13 pmPossible solutions:
Wed, Feb 22 2023 5:55:13 pm- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
Wed, Feb 22 2023 5:55:13 pm- Install for the current linuxmusl-x64 runtime: "npm install --platform=linuxmusl --arch=x64 sharp"
Wed, Feb 22 2023 5:55:13 pm- Consult the installation documentation: https://sharp.pixelplumbing.com/install
Wed, Feb 22 2023 5:55:13 pmError: Could not load js config file /opt/app/node_modules/@strapi/plugin-upload/strapi-server.js:
Wed, Feb 22 2023 5:55:13 pmSomething went wrong installing the "sharp" module
Wed, Feb 22 2023 5:55:13 pm
Wed, Feb 22 2023 5:55:13 pmCannot find module '../build/Release/sharp-linuxmusl-x64.node'
Wed, Feb 22 2023 5:55:13 pmRequire stack:
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/node_modules/sharp/lib/sharp.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/node_modules/sharp/lib/constructor.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/node_modules/sharp/lib/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/server/services/image-manipulation.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/server/services/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/server/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/plugin-upload/strapi-server.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/utils/lib/import-default.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/utils/lib/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/strapi/lib/Strapi.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/strapi/lib/index.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/strapi/lib/commands/start.js
Wed, Feb 22 2023 5:55:13 pm- /opt/app/node_modules/@strapi/strapi/bin/strapi.js
Wed, Feb 22 2023 5:55:13 pm
Wed, Feb 22 2023 5:55:13 pmPossible solutions:
Wed, Feb 22 2023 5:55:13 pm- Install with verbose logging and look for errors: "npm install --ignore-scripts=false --foreground-scripts --verbose sharp"
Wed, Feb 22 2023 5:55:13 pm- Install for the current linuxmusl-x64 runtime: "npm install --platform=linuxmusl --arch=x64 sharp"
Wed, Feb 22 2023 5:55:13 pm- Consult the installation documentation: https://sharp.pixelplumbing.com/install
Wed, Feb 22 2023 5:55:13 pmat loadJsFile (/opt/app/node_modules/@strapi/strapi/lib/core/app-configuration/load-config-file.js:18:11)
Wed, Feb 22 2023 5:55:13 pmat loadFile (/opt/app/node_modules/@strapi/strapi/lib/core/app-configuration/load-config-file.js:35:14)
Wed, Feb 22 2023 5:55:13 pmat Object.loadPlugins (/opt/app/node_modules/@strapi/strapi/lib/core/loaders/plugins/index.js:95:26)
Wed, Feb 22 2023 5:55:13 pmat async Strapi.loadPlugins (/opt/app/node_modules/@strapi/strapi/lib/Strapi.js:333:5)
Wed, Feb 22 2023 5:55:13 pmat async Promise.all (index 2)
Wed, Feb 22 2023 5:55:13 pmat async Strapi.register (/opt/app/node_modules/@strapi/strapi/lib/Strapi.js:369:5)
Wed, Feb 22 2023 5:55:13 pmat async Strapi.load (/opt/app/node_modules/@strapi/strapi/lib/Strapi.js:474:5)
Wed, Feb 22 2023 5:55:13 pmat async Strapi.start (/opt/app/node_modules/@strapi/strapi/lib/Strapi.js:212:9)
Wed, Feb 22 2023 5:55:13 pmnpm notice
Wed, Feb 22 2023 5:55:13 pmnpm notice New major version of npm available! 8.19.3 -> 9.5.0
Wed, Feb 22 2023 5:55:13 pmnpm notice Changelog: <https://github.com/npm/cli/releases/tag/v9.5.0>
Wed, Feb 22 2023 5:55:13 pmnpm notice Run `npm install -g [email protected]` to update!
Wed, Feb 22 2023 5:55:13 pmnpm notice
刚开始以为是自己的dockerfile写得有问题,我的dockerfile如下所示
FROM node:16-alpine as build
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /opt/
COPY ./package.json ./yarn.lock ./
ENV PATH /opt/node_modules/.bin:$PATH
RUN yarn config set network-timeout 600000 -g && yarn install
WORKDIR /opt/app
COPY ./ .
RUN yarn build
FROM node:16-alpine
RUN apk add vips-dev
RUN rm -rf /var/cache/apk/*
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /opt/app
COPY --from=build /opt/node_modules ./node_modules
ENV PATH /opt/node_modules/.bin:$PATH
COPY --from=build /opt/app ./
EXPOSE 8081
CMD ["yarn", "start"]
为了证明自己的dockerfile没有问题,我在本地安装了一个docker环境,运行以下命令执行代码,测试是否有异常
docker build -t web-test:v4 .| tee my-image.build.log
docker images
docker run -d -p 1337:1337 web-test:v4
docker logs --tail=1000 72fc083f1e8f14bd184f6539a64e897c6afd79ce466f76ca725761f5f6927574
如下图所示,项目在本地进行docker build ,run 一切正常,证明我的dockerfile没有问题
分析
原因一:先查看部署服务器的的项目依赖,是否是正确的node版本和npm版本,如果当前项目依赖的版本和实际环境有很大差异,会导致以上报错,对于我的项目来说,这个肯定是没问题
举个栗子:
原因二:网络不给力,导致没有下载成功包
这个就不赘述了,这个是我们最容易遇到的,也是我这次部署遇到的情况
问题解决
方案一:在项目目录下配置.yarnrc(或者.npmrc文件),下面的代码选择你需要的配置哦
registry "https://registry.npm.taobao.org"
sass_binary_site "https://npm.taobao.org/mirrors/node-sass/"
phantomjs_cdnurl "http://cnpmjs.org/downloads"
electron_mirror "https://npm.taobao.org/mirrors/electron/"
sqlite3_binary_host_mirror "https://foxgis.oss-cn-shanghai.aliyuncs.com/"
profiler_binary_host_mirror "https://npm.taobao.org/mirrors/node-inspector/"
chromedriver_cdnurl "https://cdn.npm.taobao.org/dist/chromedriver"
sharp_binary_host "https://npm.taobao.org/mirrors/sharp"
sharp_libvips_binary_host "https://npm.taobao.org/mirrors/sharp-libvips"
方案二:使用命令配置sharp的镜像地址
如果你是npm
npm config set sharp_binary_host "https://npm.taobao.org/mirrors/sharp"
npm config set sharp_libvips_binary_host "https://npm.taobao.org/mirrors/sharp-libvips"
npm install sharp
如果你是yarn
设置 sharp_binary_host镜像地址
yarn config set sharp_binary_host "https://npm.taobao.org/mirrors/sharp"
设置 sharp_libvips_binary_host镜像地址
yarn config set sharp_libvips_binary_host "https://npm.taobao.org/mirrors/sharp-libvips"
yarn add sharp
方案三:把你的服务器网络搞一下~
今天就写到这里啦~
- 小伙伴们,( ̄ω ̄( ̄ω ̄〃 ( ̄ω ̄〃)ゝ我们明天再见啦~~
- 大家要天天开心哦
欢迎大家指出文章需要改正之处~
学无止境,合作共赢