我需要安装依赖项才能在 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 [email protected] does not satisfy [email protected]
npm ERR! Missing: [email protected] from lock file
...
在本地环境中启动效果很好。如何解决此问题?
我尝试删除
node_modules
和 npm install
我遇到了这个问题,因为在我的用户设置中(在
~/.npmrc
中),legacy-peer-deps
标志被设置为true
,而在构建环境中并非如此。
是我的锁定文件导致了这个问题。首先,确保您使用的是正确的 Node 版本(无论适合您/您的团队)!
如果您愿意删除锁定文件(由于显而易见的原因,这可能是一个问题),请尝试以下操作:
$ nvm use 16
Now using node v16.20.2 (npm v8.19.4)
$ rm -rf node_modules/
$ rm package-lock.json
$ npm i
$ npm ci # should work
如果您不想删除它,请尝试以下操作:
$ nvm use 16
Now using node v16.20.2 (npm v8.19.4)
$ rm -rf node_modules/
$ npm i
$ npm ci # should work
如果
npm ci
不起作用,请立即再次运行 npm i
,然后再次尝试 npm ci
- 希望它现在可以工作!
尝试添加以下内容
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" ]
让我知道这是否有效。