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?

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.