gpt4 book ai didi

Node.js 应用程序在本地运行但在 docker 上失败(sh : 1: rimraf: not found)

转载 作者:行者123 更新时间:2023-12-02 16:11:26 28 4
gpt4 key购买 nike

我能够构建 docker 镜像,但无法让容器运行。这是 package.json:

{
"name": "linked-versions-viewer",
"version": "1.0.0",
"description": "...",
"main": "./dist/bundle.js",
"type": "module",

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "rimraf dist",
"build_only": "rollup -c",
"build": "cross-env NODE_ENV=production npm run clean && npm run build_only",
"serve": "parcel src/index.html --port 12345",
"start": "npm run clean && npm run serve"
},
"repository": {
"type": "git",
"url": "..."
},
"author": "...",
"license": "...",
"dependencies": {
"antd": "^4.12.3",
"base-widget": "...",
"react": "^17.0.2",
"react-dom": "^17.0.1"
},
"devDependencies": {
"@types/node": "^14.14.28",
"@types/react": "^17.0.2",
"@types/react-dom": "^17.0.1",
"cross-env": "^7.0.3",
"parcel-bundler": "^1.12.4",
"rev-hash": "^3.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.39.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.29.0",
"rollup-plugin-uglify": "^6.0.4",
"typescript": "^4.1.5"
}
}

这是 dockerfile:

FROM node:14.17.0 as base

ENV NODE_ENV=production

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 8080

FROM base as production

ENV NODE_PATH=./build

CMD ["npm", "start"]

我通过运行 docker build -t testapp . 创建了镜像,并尝试使用 docker run -p 8080:8080 -d testapp 运行容器,但它总是立即退出代码为 1。这是图像的错误日志:

> linked-versions-viewer@1.0.0 start /app
> npm run clean && npm run serve


> linked-versions-viewer@1.0.0 clean /app
> rimraf dist

sh: 1: rimraf: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! linked-versions-viewer@1.0.0 clean: `rimraf dist`
npm ERR! spawn ENOENT
npm ERR!
npm ERR! Failed at the linked-versions-viewer@1.0.0 clean 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/2021-06-09T20_40_17_204Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! linked-versions-viewer@1.0.0 start: `npm run clean && npm run serve`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the linked-versions-viewer@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我尝试完全删除 rimraf 依赖项,但随后出现了类似的 parcel 依赖项错误,所以可能没有正确安装包?如果有帮助,该应用程序也是用 typescript 编写的。

不确定接下来要尝试什么。感谢您提出任何建议。

最佳答案

表格npm install docs ,

With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies

因为你的 base 图像中有 ENV NODE_ENV=production,所以 rimrafparcel-bundler 都不是安装在您的容器内。

您的 npm start 命令正在运行 npm run clean && npm run servenpm run clean 使用 rimraf 模块,npm run serve 使用 parcel-bundler 模块。这就是您看到这两个错误的原因。

您可以尝试以下解决方案之一,

  1. 从您的 Dockerfile 中删除 ENV NODE_ENV=production(这是最快的解决方案,但不应在生产中使用)

  2. 您可以使用以下方法在容器内全局安装 rimrafparcel-bundler:

    RUN npm install --global rimraf && npm install --global parcel-bundler

    但是,我仍然认为这不是一个好的生产就绪设置。

  3. 您可以在容器内使用 npm run build 正确构建您的应用程序并提供服务。但是,我对 React 不够熟悉,无法帮助您在 Docker 上进行设置。

关于Node.js 应用程序在本地运行但在 docker 上失败(sh : 1: rimraf: not found),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67894366/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com