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

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

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

问题

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

Dokerfile:

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

package.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"
  },
  ...
}

在容器中遇到以下错误:

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
...

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

我尝试删除node_modules并运行npm install

英文:

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

Dokerfile:

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

package.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"
  },
  ...
}

In container I faced with this errors:

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
...

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

FROM node:18.12.1

ENV CI="true"

WORKDIR /workdir

# 删除 package-lock.json
RUN rm package-lock.json

# 安装 npm 依赖
RUN npm install

# 将 package.json 和 package-lock.json 复制到容器中
COPY package.json package-lock.json /workdir/

# 复制剩余的应用程序代码
COPY . /workdir/

# 构建你的应用程序
RUN npm run build

# 指定运行应用程序的命令
CMD [ "npm", "start" ]

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

英文:

Try to add the following Dockerfile:

FROM node:18.12.1

ENV CI="true"

WORKDIR /workdir

# Remove package-lock.json
RUN rm package-lock.json

# Install npm dependencies
RUN npm install

# Copy package.json and package-lock.json to the container
COPY package.json package-lock.json /workdir/

# Copy the rest of the application code
COPY . /workdir/

# Build your application
RUN npm run build

# Specify the command to run your application
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:

确定