A Little NPM Crash Course
Really, NPM should come installed with Node.js. This is going to be a bit of a cop out, but here, I am just going to defer to someone else’s install guide. Here’s a link to NPM’s website for Mac/Unix, and here is a link to download Node for Windows. Once that is installed, we can get started with NPM, which should be configured to use with whatever CLI tool your system.
npm init is used to, simply enough, initialize a Node project, by creating a package.json file in the current directory. Basically, package.json is a configuration file for your Node project. It stores, among other things, your package name, it’s version, and the list of packages your Node project uses. This package list is really great when we get to installing Node.js packages. When used,
npm init will provide you with a series of questions to answer about your Node project, mainly the three features mentioned earlier. For some of these prompts, default values can be used by hitting enter if no value was entered. You could, if you want, skip these by including
-y in the command, like so:
npm init -y. This would use default values for prompts.
As you might suspect,
npm install is used for installing packages. There is this really nifty feature in NPM, where if you use
npm install alone, no options, NPM will read your project’s packages.json file, and install all the packages listed in packages.json. This may seem a bit weird, but, in my exprience, Node projects are not stored with their package dependencies. So, if you clone a Node.js project repository from GitHub, you will probably be running
npm install to install it’s packages. Really speeds things up. All these dependencies will be install in the local directory in the file,
npm install package --save
This is sort of the most basic type use of install. This will install package in your project, and will also insert it in the list of dependencies in your package.json file. You use
npm install package --save to install packages that you will be used in your project. So, packages like Express, or Pug. Further, any package installed like this will only be accessible in the local project.
npm install package -g
The option -g stands for global, so this is a global install. Basically, a package installed with this will be available, well, globally. That is, you can run it most anywhere NPM can. Because of this, you really only use global installs for tools that you use with Node, like Nodemon. The official Node documentation elaborates pretty well on when to use global vs. local installs.
npm uninstall package
Actually, I rarely use this command, but it is very important to know how to use it. It is fairly straightforward, the listed package will be uninstalled. The following options are also rather important:
- using this option will also remove package from the dependencies list in package.json
- use this option to remove a global package
This will list any of your node packages that are outdated. Great for pre-empting this next command:
This will try to update all the packages listed in the current directory. Use the option
-g for global packages.
npm update package
npm update, only this will only update package. Likewise,
-g can be used to target global packages.
This is used to maintain the version of your Node project. In Node, a project is a three part code, formatted as such:
Major Version.Minor Version.Patch Version
Which creates a number like this:
This video actually goes into pretty good details on when to change each number. You can honestly manually edit the package.json file yourself, but you can also use the command
npm version with the following options to increment the respective values by one:
- Increment the patch number by one
- Increment the minor number by one
- Increment the major number by one
Note that these options are not preceded by a dash (‘-‘). That was not meant to be an emoticon, rather what I meant by dash. It also happens to work well as an emoticon.
An addendum, a very nice thing about NPM is that version info integrates quite nicely with Git. The details can be found on the NPM doc page on version.
And this concludes my little NPM Crash Course. Hope you found it useful! As this is only an NPM crash course, it may be a bit sparse in some details. So, I encourage you to refer to the official NPM documentation. Actually, it is surprisingly delightfully sarcastic at times, so I say just check it out for whatever, if you want.