Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm - save installed packages as dependencies

I've installed some packages via npm install $package, without setting up a package.json first. Now I would like to create a package.json file, but keep all installed packages as dependencies. Simply running npm init doesn't offer this option, can I achieve this automatically?

like image 254
doque Avatar asked Mar 26 '15 10:03

doque


People also ask

How do I save an npm package to dependencies?

To add dependencies and devDependencies to a package. json file from the command line, you can install them in the root directory of your package using the --save-prod flag for dependencies (the default behavior of npm install ) or the --save-dev flag for devDependencies.

What is the purpose of npm I -- save Packagename >?

json file in the dependency section. If you are using a recent version of npm save yourself from unnecessary typing and use npm install [Package Name] instead of npm install [Package Name] --save by default it will add the installed package to the dependency list in the package.

Why do we not use -- save with npm install anymore?

You don't need --save anymore for NPM installs. This was long the golden standard to install a package and save it as a dependency in your project. Meaning if we didn't specify the --save flag, it would only get locally installed and not added to the package.

What is the difference between npm install and npm install -- save?

When we run the npm install package_name command, then the package name is added in the package. json file as a dependency. The --save specifies that the package is required at production or development.


4 Answers

Update January 2016

npm now supports this out of the box. I have npm version 3.5.2.

so with just a node_modules folder with underscore installed.

npm init --yes 

then:

cat package.json 

Contained within package.json:

"dependencies": {     "underscore": "^1.8.3"   }, 
like image 126
arcseldon Avatar answered Sep 23 '22 17:09

arcseldon


UPDATE: With the launch of npm v3, this trick will create a lot of unwanted entries on your package.json file. That's because module dependencies are now flattened, as @sunny-mittal pointed out.

npm doesn't support that, as far as I know. You'd have do reinstall each package passing --save to each one.

But, there's a workaround, if your're on Unix based systems. From inside your project root folder, with a package.json file already created (npm init, as you mentioned), run:

npm install $(ls node_modules/) --save 

and it will reinstall the packages, and save them into package.json as dependencies.

like image 33
Rodrigo Medeiros Avatar answered Sep 22 '22 17:09

Rodrigo Medeiros


Since NPM node_modules is flat now and @Rodrigo's answere don't deal to well with that.

This is what I knitted together.

npm list --depth=0 | sed "1d" | sed -E "s/^[\`+-]+\s//" | sed -E "s/@(.*)$//"

This is essentially what ls node_modules did before.

One-liner to save installed.

npm install $(npm ls | sed "1d" | sed -E "s/^[\`+-]+\s//" | sed -E "s/@(.*)$//") --save

I'm using

$ npm --version 
3.5.3

Which lists like this.

$ npm list --depth=0
[email protected] /home/victor/x
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
`-- [email protected]
like image 40
Victor Häggqvist Avatar answered Sep 20 '22 17:09

Victor Häggqvist


I wrote a module called pkg-save.
You can have a try if your npm version is "2.x.x".
I haven't test in npm v3, so I don't know whether it is useful or not in npm v3.

like image 20
Sinalvee Avatar answered Sep 22 '22 17:09

Sinalvee