Interactively create a package.json file

Until recently I had always thought that package.json files for the node package manager had to be created manually which was a long winded process. Turns out this is not the case and by running a single command it will ask you a bunch of questions, and then write a package.json for you.

npm init [-f|--force|-y|--yes]

It attempts to make reasonable guesses about what you want things to be set to, and then writes a package.json file with the options you’ve selected. If you already have a package.json file, it’ll read that first, and default to the options in there.

It is strictly additive, so it does not delete options from your package.json without a really good reason to do so.

The additional parameters in the square brackets are optional -f, --force, -y, --yes if you invoke these it will use only defaults and not prompt you for any options.

Then, once you have a package.json file, there are a handful of useful commands which I’ve listed out below.

Install a package

Install the latest version of the package and automatically add it to your package.json’s dependencies list.

npm install <pkg> --save-dev

Install a specific version

Install a specific version of a package.

npm install <pkg>@<version> --save-dev

Update packages to their latest version

Update packages into the package.json file and automatically add updated version for all packages under dependencies into package.json file.

npm update --save-dev

Uninstall a package

Automatically remove a package from dependencies in the package.json file and remove the package from the node_module folder.

npm uninstall <pkg> --save-dev

For more information on this topic please refer to the npm documentation. Do you have any additional commands that are useful?