`npm ERR!` `npm ci`只能在package.json和package-lock.json文件存在时安装包。

huangapple go评论121阅读模式
英文:

npm ERR! `npm ci` can only install packages when your package.json and package-lock.json

问题

需要在CI/CD中安装依赖项以构建前端。

Dokerfile:

  1. FROM node:18.12.1
  2. enter code here
  3. ENV CI="true"
  4. WORKDIR /workdir
  5. COPY package.json package-lock.json /workdir/
  6. RUN npm ci

package.json:

  1. {
  2. "name": "v2",
  3. ...
  4. "dependencies": {
  5. "formik": "^2.2.9",
  6. "i18next": "^21.9.1",
  7. "lodash": "^4.17.21",
  8. "react": "^18.2.0",
  9. "react-dom": "^18.2.0",
  10. "react-router-dom": "^6.3.0",
  11. "react-scripts": "5.0.1"
  12. },
  13. "devDependencies": {
  14. "typescript": "^4.9.5"
  15. },
  16. ...
  17. }

在容器中遇到以下错误:

  1. npm ERR! code EUSAGE
  2. npm ERR!
  3. npm ERR! `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.
  4. npm ERR!
  5. npm ERR! Invalid: lock file's ajv@6.12.6 does not satisfy ajv@8.12.0
  6. npm ERR! Missing: ajv@6.12.6 from lock file
  7. ...

在本地环境中启动工作正常。如何修复此行为?

我尝试删除node_modules并运行npm install

英文:

I need to install dependencies to build the frontend on CI/CD.

Dokerfile:

  1. FROM node:18.12.1
  2. enter code here
  3. ENV CI="true"
  4. WORKDIR /workdir
  5. COPY package.json package-lock.json /workdir/
  6. RUN npm ci

package.json

  1. {
  2. "name": "v2",
  3. ...
  4. "dependencies": {
  5. "formik": "^2.2.9",
  6. "i18next": "^21.9.1",
  7. "lodash": "^4.17.21",
  8. "react": "^18.2.0",
  9. "react-dom": "^18.2.0",
  10. "react-router-dom": "^6.3.0",
  11. "react-scripts": "5.0.1"
  12. },
  13. "devDependencies": {
  14. "typescript": "^4.9.5"
  15. },
  16. ...
  17. }

In container I faced with this errors:

  1. npm ERR! code EUSAGE
  2. npm ERR!
  3. npm ERR! `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.
  4. npm ERR!
  5. npm ERR! Invalid: lock file's ajv@6.12.6 does not satisfy ajv@8.12.0
  6. npm ERR! Missing: ajv@6.12.6 from lock file
  7. ...

Start on local environment works fine. How to fix this behavior?

I've tried remove node_modules and npm install

答案1

得分: 2

因为在我的用户设置中(在 ~/.npmrc 中),legacy-peer-deps 标志被设置为 true,而在构建环境中并非如此,所以我遇到了这个问题。

英文:

I ran into this problem because in my user settings (in ~/.npmrc) the legacy-peer-deps flag was set to true, while it wasn't so in the build environment.

答案2

得分: -1

尝试添加以下的 Dockerfile

  1. FROM node:18.12.1
  2. ENV CI="true"
  3. WORKDIR /workdir
  4. # 删除 package-lock.json
  5. RUN rm package-lock.json
  6. # 安装 npm 依赖
  7. RUN npm install
  8. # 将 package.json 和 package-lock.json 复制到容器中
  9. COPY package.json package-lock.json /workdir/
  10. # 复制剩余的应用程序代码
  11. COPY . /workdir/
  12. # 构建你的应用程序
  13. RUN npm run build
  14. # 指定运行应用程序的命令
  15. CMD [ "npm", "start" ]

如果这个可以的话,请告诉我。

英文:

Try to add the following Dockerfile:

  1. FROM node:18.12.1
  2. ENV CI="true"
  3. WORKDIR /workdir
  4. # Remove package-lock.json
  5. RUN rm package-lock.json
  6. # Install npm dependencies
  7. RUN npm install
  8. # Copy package.json and package-lock.json to the container
  9. COPY package.json package-lock.json /workdir/
  10. # Copy the rest of the application code
  11. COPY . /workdir/
  12. # Build your application
  13. RUN npm run build
  14. # Specify the command to run your application
  15. CMD [ "npm", "start" ]

Let me know if this works.

huangapple
  • 本文由 发表于 2023年6月2日 13:58:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定