I downloaded a theme and it has a package-lock.json file but no package.json file. Is there a way I can generate the package.json from the package-lock.json file. How do I install the node modules with just the package-lock.json file. Is there a way to do that?
5 Answers
Warning: Do not attempt before reading comments below & backup package-lock.json.
Install the latest npm with npm install -g npm
Run npm init and respond to the questions.
The above command will generate a package.json and include the existing packages listed in package-lock.json
10 Comments
6.4.1) and it doesn't work. It only creates a package.json file without any dependencies listed.6.6.0. package.json does not include the existing packages listed in package-lock.jsonI think I figured it out.
I don't think npm init can draw from package-lock.json. However it does seem to pull from what is already in your /node_modules. I believe this is why @Harry B's solution works for some and not at all for others.
For example, if you have just cloned your project which contains package-lock.json, no package.json, and empty/non-existence node_modules, npm init won't create any dependencies. However, if you run npm install pkg1 pkg2 pkg3 ... then run npm init it will create the dependencies in package.json.
2 Comments
https://pravnyadv.github.io/unpackage/ seems to work. Copy your package lock file text in, hit the button, copy out the text into a new package.json file.
5 Comments
package.json a single line. If so and you'd like to unfurl it a multi-line format, pipe it into jq with: echo '$your_package_blobl' | jqpackage-lock.json file relies on the presence of a package.json file, So it's not possible to retrieve package.json (happy to be proved wrong).
So a possible solution left is to use a module like auto-install which is capable of generating package.json from the project file dependencies.
First, you need to install the module globally npm install -g auto-install. Then run npm init and answer the basic requirements.
Then, run auto-install in your project root directory. All the dependencies should reflect in package.json file.
**
Or Install node modules directly from package-lock.json
**
Run npm ci which bypasses a package’s package.json to install modules from a package’s lockfile.
2 Comments
npm ci always tells me "added 0 packages" and auto-install rewrote my package-lock.json, installed 2 random packages and stopped at "Watchers initialized"... node_modules folder was created but only with these 2 packages... Glad I had a lock backup :(i know this is a very old question, but all of these answers are sooo close to solving the problem, yet none of them worked for me because my node_modules folder was empty. they did however lead me to an answer, which is basically a combination of the previous suggestions.
preliminary boilerplate:
before doing anything, make a backup of your package-lock.json, just in case, and run npm install -g npm just in case you're wildly out of date, although this may not be necessary... i am using npm 8.19.4 because that's what it happens to be for me.
first:
run npm ci
this will generate the node_modules that you need, but then it will complain about a missing package.json and appear to fail. don't worry, that's fine.
second:
run npm init and answer all the questions, you can literally just hit enter to each one because whatever.
this will create your package.json, and now it should be fully populated with the node_modules that were generated by the first step.
third:
profit!
npm install?