npm 错误!`npm ci` 只能在您的 package.json 和 package-lock.json 时安装软件包

pla*_*sss 0 frontend docker dockerfile create-react-app cicd

我需要安装依赖项才能在 CI/CD 上构建前端。

文档文件:

FROM node:18.12.1
enter code here
ENV CI="true"
WORKDIR /workdir
COPY package.json package-lock.json /workdir/
RUN npm ci
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "v2",
  ...
  "dependencies": {
    "formik": "^2.2.9",
    "i18next": "^21.9.1",
    "lodash": "^4.17.21",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.3.0",
    "react-scripts": "5.0.1"
  },
  "devDependencies": {
    "typescript": "^4.9.5"
  },
  ...
}
Run Code Online (Sandbox Code Playgroud)

在容器中我遇到了这个错误:

npm ERR! code EUSAGE
npm ERR! 
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.
npm ERR! 
npm ERR! Invalid: lock file's ajv@6.12.6 does not satisfy ajv@8.12.0
npm ERR! Missing: ajv@6.12.6 from lock file
...
Run Code Online (Sandbox Code Playgroud)

从本地环境开始工作正常。如何解决这种行为?

我试过删除node_modulesnpm install

pla*_*sss 5

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