Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a pip freeze equivalent for Node and NPM?

Tags:

node.js

npm

This is idiomatic in Python:

pip freeze > requirements.txt pip install -r requirements.txt 

The first command saves a list of requirements to a file. Then later you can use the command to install the requirements into your environment.

Node has npm install, but I don't get how to I'm supposed to dump the state of my dependencies to a package.json. I Googled and found this:

npm ls | grep -E "^(├|└)─" | cut -d" " -f2 | awk '{FS = "@"; print "\""$1"\"", ":", "\""$2"\""}' 

but as the author of this pipeline suggests, there's got to be a better way? What am I missing here?

I just want to dump my current deps into a package.json. As https://npmjs.org/doc/shrinkwrap.html says,

The "package.json" file is still required if you want to use "npm install".

I've skimmed the info on shrinkwrap, but I'm not seeing how to simply accomplish this task with shrinkwrap.

like image 227
Dmitry Minkovsky Avatar asked Jul 10 '13 16:07

Dmitry Minkovsky


People also ask

Is pip similar to npm?

npm is the command-line interface to the npm ecosystem. It is battle-tested, surprisingly flexible, and used by hundreds of thousands of JavaScript developers every day. On the other hand, pip is detailed as "A package installer for Python". It is the package installer for Python.

Does pip freeze show dependencies?

Since pip freeze shows all dependencies as a flat list, finding out which are the top level packages and which packages do they depend on requires some effort. It's also tedious to resolve conflicting dependencies that could have been installed because older version of pip didn't have true dependency resolution [1].

Can you freeze pip?

The format of pip freeze is the format for requirements. txt , which is a configuration file for installing packages in bulk. If you output pip freeze as a file with redirection > and use it in another environment, you can install the same packages at once.

How do you freeze a requirement in pip?

The most common command is pip freeze > requirements. txt , which records an environment's current package list into requirements. txt. If you want to install the dependencies in a virtual environment, create and activate that environment first, then use the Install from requirements.


2 Answers

This is the closest I got

npm freezelol

npm ls | grep -E "^(├|└)─" | cut -d" " -f2 | awk -v quote='"' 'BEGIN { FS = "@" } ; { print quote $1 quote,":",quote $2 quote"," }' | sed -e 's/ :/:/g'

Output is like

  "bower": "1.3.12",   "chai": "2.1.2",   "cucumber": "0.4.8", 

Still needs to trim the final trailing comma but it's pretty close!

like image 169
Ralph Cowling Avatar answered Sep 18 '22 08:09

Ralph Cowling


You can create a package.json out of the currently installed package using npm init. Then you can easily move the package.json and simply do npm install to install the packages wherever you want.

like image 31
user568109 Avatar answered Sep 21 '22 08:09

user568109