83

I accidentally deleted my package-lock.json file. npm install is not generating a new one. How do I get npm to recreate this file.

Dblock247
  • 6,167
  • 10
  • 44
  • 66
  • what node version are you using? – emil Aug 24 '17 at 16:16
  • @emil I am using v6.10.2 – Dblock247 Aug 24 '17 at 16:21
  • I think below 8.0, they use shrinkwrap.json instead of package-lock.json – emil Aug 24 '17 at 16:23
  • 1
    @emil it originally created a package-lock.json and I don't have a shrinkwrap.json either. Never seen it before. – Dblock247 Aug 24 '17 at 16:24
  • 2
    Also, `npm` only creates/updates the lock file when it modifies your `node_modules` folder, so in *theory* you could delete your `node_modules` folder, run `npm i` and it should regenerate the file for you – wjvander Aug 24 '17 at 16:25
  • Yes. if you remove node_modules and reinstall with node version 8.0 above, you will see lock file. – emil Aug 24 '17 at 16:27
  • 1
    @emil I just upgraded to v8.4.0 and ran npm install and it created the package-lock.json – Dblock247 Aug 24 '17 at 18:17
  • 1
    @emil, a proper answer would go a long way to resolving this question. – isherwood Jan 23 '18 at 17:35
  • 14
    For other people experiencing this problem: also ensure that .npmrc does not contain `package-lock=false`. This is another reason why `package-lock.json` might not exist. – Kevin May 23 '18 at 17:09
  • 1
    **Check your `.gitignore`.** I accidentally had `package-lock.json` in the `.gitignore` somehow and because `package-lock.json` wasn't showing up in the `git status` it was throwing me off. – Joshua Pinter Mar 19 '19 at 15:29
  • None of the solutions so far has worked for me. I use node v16.14.* . I am running this in a sub-package ( workspace ), but it should work regardless, because it has a local `package.json` in the subfolder that my current working directory is. – Sohail Si Jun 22 '22 at 08:53

6 Answers6

97

There might be a file called .npmrc which can contain

package-lock=false

which will cause the package lock file to not be generated.

In theory you could also have turned on npm config set package-lock false globally (change to true to turn on again), but that's less likely to happen unintentionally.

David Mulder
  • 26,123
  • 9
  • 51
  • 114
  • 3
    Yep, this is it. Thanks for adding this as a proper answer. – Joshua Pinter Nov 29 '18 at 18:41
  • 1
    What's the purpose of this? I'm working with a project that requires running `npm ci` which fails because there's no package-lock file - so why would the maintainers add this setting to npmrc? – Matt Feb 10 '21 at 21:01
  • 1
    @Matt Some people do not see the benefits of version lock files and thus hate the fact that it generates a new lock file each time that 'pollutes' their commits from their perspective. – David Mulder Feb 10 '21 at 23:36
  • Can this also be specified in package.json (without additional files). I mean, it's about package.json. It's a required file to install node packages... – Domske Apr 09 '21 at 13:15
53

The package-lock.json file was introduced in npm v5, so the steps you need to take to regenerate package-lock.json depend on which version of npm you're using.

FYI. Let's verify what version of node and npm.

npm -v

prints: x.x.x

node -v

prints: x.x.x

I believe for package-lock.json is auto-generated if the 2 conditions npm version > 5.x.x and node version > 7.x.x are met

Then, try the following steps depending on which version you have:

npm v5+:

Regenerate the package-lock.json by running npm install. You may also regenerate the file without actually downloading dependencies by using npm install --package-lock-only

npm v4.x & earlier:

  1. Generate a npm-shrinkwrap.json by running npm shrinkwrap. This file has the same format as package-lock.json and achieves essentially the same purpose in earlier versions of npm (see https://docs.npmjs.com/files/package-lock.json and https://docs.npmjs.com/files/shrinkwrap.json for detailed information on this point)
  2. Rename the npm-shrinkwrap.json to package-lock.json
James
  • 173
  • 2
  • 10
17

To resolve this issue I have tried below mentioned things and it worked for me :

Make sure your package-lock globally enabled, you can enable it using:

npm config set package-lock true

To make sure your .npmrc is not blocking the creation of package-lock file, set this to your .npmrc

echo 'package-lock=true' >> .npmrc

note: package-lock.json is automatically generated for any operations where npm modifies either the node_modules tree, or package.json for npm -v > 5.x.x.

check your npm version: npm -v

update your npm to latest version using:

npm install -g npm@latest

npm i -g npm-upgrade

@will

Bindu
  • 181
  • 1
  • 4
1

Make sure you are in the right folder in the command line (use pwd in Linux/macOS to get the current path you're in).

I've run npm install many times, just to find out later I was doing it in the wrong folder.

A-S
  • 2,547
  • 2
  • 27
  • 37
-1

I was also facing the same issue

I just removed the package-lock=false from .npmrc and now it is creating the lock file

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Shyam Kumar
  • 586
  • 3
  • 17
-2

If your npm version is <5 you will have a shrinkwrap.json file created when running npm install.

Otherwise package-lock will be created on npm versions 5 and above.

jabu.hlong
  • 2,194
  • 20
  • 21
  • 1
    It generates nothing after running `npm i` for me. Using node 6.12.2 npm 3.10.10 – Mike Milkman Jul 02 '18 at 22:05
  • don't know if this depends that much on the npm version, since for my project we use version 14.xx and it still generated a `shrinkwrap.json` most probably some setting. But it's worth checking if there is such a file if you can't find the `package-lock.json` – cloned Mar 07 '23 at 14:59