At first, these libraries are few and easy to maintain; however, soon enough dependency hell sets in and a more mature solution is required.
Admittedly, most use cases stop at an
npm install, and there isn’t a need for anything else. However, npm has loads of additional features, which I’ll walk you through, highlighting those I consider essential, really useful, or just plain awesome.
Here are the core commands that stand out.
Note that most of these commands can take subcommands and/or configurations, and this list is by no means an end-all discussion on the CLI.
Configuration is a major part of
npm, and there are multiple ways configuration variables can be set.
Configuration via CLI and Environmental Variables
Configuration via the
When persisted configuration needs to happen on the project level, or on the built-in level, the
.npmrcfile must be modified using a text editor.
Configuration via package.json
Finally, configuration can be set from the
package.jsonfile. However, this is seldom used (and should only be used if explicitly required), because a project level
.npmrcfile is the conventionally prefered place to set package configuration.
Notable Configuration Settings
Updating npm Using npm
npm as a standalone tool, make sure you understand the implications of using whatever version you choose. There is a great tool for managing different Node.js versions (and in turn npm versions) on the same system called nvm.
The package.json File
Over and above that, you can have a list of dependencies a mile long, and define a specific version to be used for each of them, using the semver versions and range operators. Here’s a list of other notable properties.
main defines the entry point to your application, which defaults
to . Depending on convention or your framework, it might be or . You can, of course, make it anything you want.
Second, it provides a place where you can alias an array of frequently used commands, ranging from build tasks (defined in gulp or grunt), triggering the installation of other dependencies (with something like bower), starting a development server with
webpack, or running a set of bash commands.
A broken package install can prove to be quite difficult to resolve, especially when the minified version of the package is used.
Separate from the dependencies property, the devDependencies property allows you to define dependencies that are only used during the development phase and are not required for the production build (like ESLint, grunt-
contrib packages, and Protractor). Just as with dependencies, this property can be modified from the terminal by adding the command or the command. The same caution applies to versioning as mentioned under dependencies.
--save-dev(or the shorthand
-D) to the end of
This is where you can specify your package’s executable file(s), like the path to a CLI utility. This property tells npm to create local or global symlinks to your executables when your package is installed.
As discussed earlier, this is where you define configuration settings
though your file.
If your intention changes, simply change the setting to false, and you will be able to publish your package.
Developing Your Own npm Package
You can also use the command line interface and
call , where update_type is either , , or , as described by semver, and this then automatically increments the version number in the file.
npm version <update_type>
While it is complex to master, it’s very rewarding.
The Power of npm
For more interesting articles on
npm, consider reading Using Scala.js with npm and Browserify.
This blog is listed under Development & Implementations Community
Share your perspective
Share your achievement or new finding or bring a new tech idea to life. Your IT community is waiting!