From Novice To (npm) Ninja In 14 Steps

tarihinde gönderildi - Son değiştirilme

If you have ever used npm in any of your projects then you understand how easy it can be to set up your development environment through the terminal. However, many people just tend to stick to the common npm init, npm install, and npm run commands for obvious reasons (mainly due to the difficult learning curve involved).

Here’s a compiled list of the best tips that will turn you into a npm ninja overnight!

1. Finding your way around npm

You can easily find npm’s online documentation, but it can be a daunting task having to constantly switch between your command line interface and your browser whenever you’re looking for something. An easier way to remember forgotten npm commands is by typing “npm help” from the command line. It will provide all the npm command lines you can use for your project.

You can also look for specific commands by typing npm help <specific command> (replace specific command with your own) e.g. npm help install.

2. A simpler approach to npm init command

Every time you want to initialize your project folders, you can type the command “npm init.” You can get bored having to feed the same information every time you run this command, but there is a nice tweak that lets you skip the information bit by using defaults. To do this you have to run the command npm init “y”.

A better way of doing this would be to set your own defaults that you would install when npm init is run. This is by making use of the “config set” command and the author name or email i.e.

npm config set $name

npm config set $email

3. Autocomplete feature

If your system uses bash or Z shell then you can take advantage of npm’s autocomplete feature. Before you’re able to use this feature, you have to enable it by typing: npm completion >> ~/.bashrc for a bash system or npm completion >> ~/.zshrc for Z shell and then running the command source ~/.bashrc.

Once enabled, you can now type commands such as “npm ins” and hit tab for it to be autocompleted to “npm install”. This is a great skill for your next freelancer project.

4. Updating npm

You can update your npm easily through the command line. Before you update it, you must check whether it needs to be updated in the first place. This is done by checking its version number i.e. by running the command npm “v”. If you are running the latest version of npm, there is no need for updating. If there are newer versions of npm, it’s recommended you update your current install. This is done by running the command “npm install -g npm”.

5. Package management

You can easily manage your packages through npm, something most people have never come to terms with. By running the command npm list, you get to see all the packages and dependencies you have installed. This command will show you everything, even sub-packages. To limit the packages you want to see, you can run “npm list -depth=0” to only see top level packages.

6. A fix for global module permissions

It can be stressful trying to install global packages to your environment. This is because of the endless permission errors you will get along the way. One of the fixes that many people have used is through the use of the sudo command. This can be quite dangerous, at the very least. A better (and safer) approach is by changing npm’s default directory to one that you currently own. This is done by running:

mkdir ~/.npm-global

npm config set prefix '~/.npm-global'

Then, depending on whether you have bash or Z shell, you can run the command: ~/.bashrc or ~/.zshrc respectively and add the line export PATH="$HOME/.npm-global/bin:$PATH" to it.

You can then reload the shell configuration file by running “source ~/.bashrc” and reinstalling/updating npm by running the command “npm install -g npm.”

7. A better way to search for packages

There are a lot of packages available on npm, and this number increases by the day. You might find great packages for use in your environment, but there is a fair share of buggy ones as well. When it comes to packages, npm has a smooth way of handling them. Instead of having to search Github for package repositories, you can simply use features like npms, packages by PageRank or npm Discover. These services allow you to search for commonly used packages that may be beneficial to your development.

8. Managing your dependencies when switching your environment to production

Whenever your project becomes fit for production, you can tag the production-ready version with the flag --production.  By doing so, the flag will ignore your dev Dependencies and instead install dependencies, thereby ensuring the development packages and tools you made use of don’t go to the production environment.

Once you have mastered this skills, you may be sure to find web development projects on where you can use your new skills.

9. Outdated dependencies management

You can easily check npm for dependencies that are outdated by running the command “npm outdated”. You can also run this command within your project in order to check which dependencies are outdated, their current version and the latest version available. This makes it easier to manage your environment.

10. Opening the homepage of a package

You can open up the homepage of a package by running the command npm home $specific_package (replace specific_package with package name). You can also check the homepage of packages that are not installed in your project, or globally on your machine.

11. Using npm prune

The command npm prune is an interesting way of finding out which modules or packages are not in your package.json file. It lists the packages, and then removes the packages that had either not been included in the package.json file, or had been npm installed through the -save flag.

12. Opening a package’s github repository

You can open a package’s Github repo simply by running npm repo $specific_package (replace specific_package with package name). Just like the home command, you don’t need to have the package installed directly in your project or globally on your machine.

13. Allow changes to the default save prefix

You can change the default save prefix for your projects easily by running the command npm config set save-prefix="~". By default, npm uses the caret (^) whenever you install new packages that have save or save-dev flags.  The difference between the tilde (~) and the caret (^) is that the tilde installs minor versions which include patches whereas the caret installs major versions (i.e. allows the minor versions to be installed with npm update).

14. Development packages

Sometimes you don’t want to publish packages you are developing directly to the registry. But - you might want to try them out in other projects you may have, by running them from other directories. There’s a simple way to do this. You can run “npm link” while in the package folder, thereby linking it to the global folder.

With that done, you can now use the package in projects by running the command require, or including it in the package.json file.

With these 14 tips, you can now manage your projects effectively using npm. If you have think we missed any points, feel free to let us know in the comments!


İlan Edilmiş 31 Ağustos, 2017


Software Developer

Lucy is the Development & Programming Correspondent for She is currently based in Sydney.

Sonraki Makale

11 Hot Tips To Enhance Your Spark Experience