35

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?

doque
  • 2,723
  • 6
  • 27
  • 44

5 Answers5

36

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"
  },
arcseldon
  • 35,523
  • 17
  • 121
  • 125
  • hmm, when i run this it doesn't do anything. although, my node_modules directory is in an ancestor folder, so that may be why – Michael May 29 '18 at 03:17
25

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.

Rodrigo Medeiros
  • 7,814
  • 4
  • 43
  • 54
  • 4
    It should be noted that with npm 3+, dependencies are flattened quite a lot and the `ls node_modules` trick, while super clever, will end up creating a lot of unwanted entries. – sunny-mittal Oct 23 '15 at 20:32
4

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
x@0.1.0 /home/victor/x
+-- babel-eslint@5.0.0-beta6
+-- babel-preset-es2015@6.3.13
+-- gulp@3.9.0
+-- gulp-babel@6.1.1
`-- gulp-eslint@1.1.1
Victor Häggqvist
  • 4,484
  • 3
  • 27
  • 35
  • In my case, `npm install $(npm ls | sed "1d" | sed -E "s/^[\`+-]+\s//" | sed -E "s/@(.*)$//") --save` didn't work because the tree representation was being returned, so I ran `npm install $(ls node_modules/ | sed "1d" | sed -E "s/^[\`+-]+\s//" | sed -E "s/@(.*)$//") --save` and it worked like a beauty. Thank! – Abhik Banerjee Feb 23 '20 at 05:13
2

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.

Sinalvee
  • 46
  • 2
-3

I faced this issue when I cloned a new project from bitbucket. I solved this by the following steps:

  1. Go to root folder where package.json exists in your project terminal.
  2. Then run the following command.

$ npm install

  • That is an entirely different issue. In my original post, the `package.json` file was missing, but modules were located in `node_modules`. What you're describing is the regular workflow for installing modules based on an existing `package.json`. – doque Jul 14 '17 at 11:20