Just like anyone else, developers enjoy knowing some little tricks to show to their colleagues or friends. Today, I have decided to write about some of these keyboard magic that you can use when working with the Node Package Manager (NPM). These won’t only impress you and the people around you, but will also make your workflow definitely faster and more concise. So now that we made this little premise, let’s start to analyze these tricks!
#1 Package initialization
Do you want to quickly initialize a new package? Are you tired of those infinite system questions that appear when doing so? Then run npm init-y or npm init-f.These commands will allow you to skip those questions and jump straight into your next amazing project!
#2 Opening a package’s homepage
Are you working with a cool npm package you just discovered? Do you want to check its homepage to read the documentation? Then run npm home“your-package”.This command will bring you directly to the page you were looking for!
#3 Opening a package’s GitHub Repository
Similarly to the command described above, running npm repo“your-package” will take you directly to a webpage. But this time, you will get automatically redirected to the package official GitHub repository.
#4 List all the installed packages
Why should we waste our time by opening the package.json file to check what packages we have installed? Simply run npm ls - - depth 0 and you will get the informations you were looking for.
#5 List the available scripts
New project new scripts right? but again, having to go through the package.json file can be tedious and time-consuming. Let’s get this list by running the npm run command instead!
#6 Check packages that are not declared in package.json
Another useful command, use npm prune for performing this operation. This command will run through your package.json file and compare its content with the /node_modules content. You will receive as an output a list of all those packages that are not in the package.json file. After doing this, npm prune will remove those packages together with those which you haven’t added manually to package.json or that were npm installed without the --save flag.
#7 Bump a package version
Need to bump a package? Simply run npm together with minor/major/patch command.
#8 Running a test
You are probably already running the npm run test command many times a day right? but why not using the shorter version npm t which is exactly the same!
Bonus commands
Still hungry for some more npm useful command shortcuts? Then check out this bonus list I have prepared for you.
Package installation
Global package installation
Package installation + saving as a dev dependency
Package installation + saving as a dependency
Conclusion
Being able to impress your colleagues is great, but leaving this article being aware of all the tricks you can learn when working with a technology is even better. Hope this article helped you to discover some tricks you didn’t know about before reading it and that it inspired you to go deeper when learning something new!
To be up-to-date with content like this, don't forget to stay tuned with my blog: TheCoderSwag.com
Top comments (29)
Also,
npx
. 😎With the latest version of node, you can just do
npx mocha
instead of having to add./node_modules/.bin/
to yourPATH
.Nice - repo will come in handy I'm sure.
npm outdated
is another good one I use a lot! Gives you a table of installed and latest versions of your packages.And you can use
nom outdated -g
to see global packages.PS.: It's broken in some versions of NPM, so update your NPM.
Thanks for the list!
npm repo
andnpm home
was new to me and something I will use really often because if we are honest: no one bookmarks the docs of a package, we hit google and hope it is under the top results 😁glad to hear it will speed up your workflow!
The -S param is still needed for confuse and impress your colleagues
Many thanks for
repo
, TIL :)I'd add
ci
andoutdated
. ;)Oh, I didn't know about
npm version patch
andnpm t
. Very cool!I wonder if you can run other scripts when bumping the version of a package (like publishing it to npm automatically and generating the changelog)
I use np for publishing.
github.com/sindresorhus/np
Oh, this is a live saver! Thanks for sharing it :)
Thanks for your blog to teach us so many tricky things, May I ask how you make those pictures?
Thank you so much for your words, I use an online tool called carbon.now.sh/. You can use it to make cool code images and set the color, background etc.
Thanks man.
npx npkill is a great one to free up hard drive space. Searches your system for
node_modules
directories, displays their sizes, and allows you to quickly delete them.I found
npm set progress=false
to help me when usingnpm
.npm t
will be greatly welcomed to my npm vocabulary. Thanks!It’s literally ‘npm test’, in the same way as ‘npm start’.