npm WARN old lockfile The package-lock.json file was created with an old version of npm – Npm

Photo of author
Written By M Ibrahim
boot2docker dockerfile node.js npm

Quick Fix: Use npx to run specific commands with an older npm version. Example: npx npm@6 ci runs npm ci using npm version 6, even if you have version 7 installed.

The Problem:

You are using Docker to build a Node.js application, and you’re encountering a warning message that says, "npm WARN old lockfile The package-lock.json file was created with an old version of npm." despite trying different npm commands and adding flags like "–package-lock." The warning appears during the "npm ci" step in your Dockerfile, and you’re unsure how to resolve it or if it can be ignored.

The Solutions:

Solution 1: Ignore the warning

The warning does not affect the installation of modules and can be safely ignored.

Solution 2: Regenerate the package-lock.json file

Run npm install --package-lock-only to regenerate the package-lock.json file. Commit the updated version of package-lock.json to your repository or Docker image.

Solution 3: Downgrade npm to an older version

Downgrade npm to an older version in production. Consider running npm version 6 as that is what ships with the current LTS version of Node.js. You can omit the RUN npm -g install [email protected] line from your Dockerfile and use the version of npm that is installed with the Docker image.

Solution 4: Use npx to run an older version of npm

If you already have a version of npm installed but want to run a specific command with an older version, you can use npx. For example, npx npm@6 ci would run npm ci with npm version 6, even if you have version 7 installed.

Solution 3: Update NPM on Your Machine

To resolve the warning, update NPM on your machine by running the below command in your local environment:

npm i -g npm

Once NPM is updated, rebuild the Docker image, and the issue should be resolved. You can still encounter the warning message, but the build process should proceed without any impediment.

Solution 4: Downgrade npm version

The warning “npm WARN old lockfile” is likely caused by a bug in npm version 7.19.1. To resolve this issue, you can downgrade to an older version of npm.

Below is a list of all npm versions where you can find older versions:

https://www.npmjs.com/package/npm?activeTab=versions

To install a specific version of npm, using the following command, replacing “V” with your desired version.

npm install -g npm@"V"

Solution 5: Remove the line `RUN npm -g install [email protected]` from your Dockerfile.

The warning/problem is with the line:
“`lang-none
RUN npm -g install [email protected]
“`
Removing this line should fix the problem/warning.

Explanation:

The package-lock generated which is part of your source repository ideally* is generated with npm version < `npm@7` which ships with Node.js <= `[email protected]`. For example Node.js LTS `v14.17.1` ships with `[email protected]`.

`npm@5`, `npm@6` generate `package-lock@v1`, which is now a legacy release. And `npm@7` which is the latest release generates `package-lock@v2`. When you do: `npm -g install [email protected]`. It overrides your existing `package-lock@v1` with `package-lock@v2` giving out the warning in the process. `npm WARN old lockfile The package-lock.json file was created with an old version of npm`

The updated **Dockerfile** should look like this:
“`lang-none
FROM node:14.17.1-alpine3.13 AS builder
WORKDIR /usr/src/app
COPY package.json package-lock.json* ./
COPY ui-runner/package*.json ./ui-runner/
COPY .npmrc .npmrc
COPY ui-runner/.npmrc ./ui-runner/.npmrc
RUN npm ci –production –package-lock && \
npm ci –production –package-lock –prefix ./ui-runner
RUN rm -f .npmrc && \
rm -f ui-runner/.npmrc

FROM node:14.17.1-alpine3.13
WORKDIR /usr/src/app
RUN apk update && apk add –no-cache curl bash
RUN addgroup -g 1001 test &&
adduser -S -u 1001 -G test test
RUN chown -R test /usr/src/app &&
chmod 755 /usr/src/app
COPY –from=builder /usr/src/app /usr/src/app
COPY . .
RUN npm run build:docker
USER test
EXPOSE 3000 9183
CMD [ "npm", "run", "start:ui-runner" ]