DEV Community

Cover image for NPM Tips and Tricks
Alex Patterson for CodingCatDev

Posted on Edited on Originally published at codingcat.dev

NPM Tips and Tricks

Original: https://codingcat.dev/post/npm-tips-and-tricks

β–Ά Watch on YouTube & Subscribe to CodingCat.dev

**NPM Tips and Tricks **is a technical guide on CodingCat.dev by Alex Patterson focused on Node.js, DevOps, JavaScript. Adding NPM Globally, setting default values for packages, and stopping the dreaded sudo calls. NPM Tips and Tricks - Installing NodeJs - .npmrc config: πŸŽ“ Lesson: https://ajonp.com/lessons/3-npm/ Ever wonder why you get EACCES without npm sudo? We can fix that and also provide other quick tips. ~~

Please note in order for you to run commands, you **must** update your .bash_profile

Then you can restart Terminal by either closing and restarting, or, In today's video, we'll be covering quick tips with NPM. After installing Node and NPM freshly, you'll typically try to install global package and get this EA access failure. We'll want to correct this. NPM comes with a nice configuration file called {dot} NPMRC that you can locate in your home folder. Currently, mine's empty. What we're going to do is add this line having a prefix home {dot} NPM... Watch the full video breakdown on YouTube or explore the architectural notes, chapters, and official documentation links below.

NPM Global install fail

Ever get the dreaded EACCES error after running npm install -g? You're not alone!

You will notice that npm is trying to install its packages to this path:

Missing write access to /usr/local/lib/node_modules

We need to change this to a better path that you have the rights to upate.

.npmrc update

using vim or nano update your local .npmrc file

vim ~/.npmrc

Update this file with the below, this will tell npm to install packages locally to .npm-packages

prefix=${HOME}/.npm-packages

Enter fullscreen mode Exit fullscreen mode

NPM Global install success

Once you change the .npmrc file, you will begin to install packages to ~/.npm-packages. No more issues

NPM init defaults

If you start projects using npm often enough you will want to default some of the authoring items. The basic syntax is npm config set init.*

Don't stress out if you are updating using npm config set while in a different directory this will still update in ~/.npmrc

npm config set init.author.name "Alex Patterson"
npm config set init.author.email "developer@ajonp.com"
npm config set init.author.url "https://ajonp.com/"
npm config set init.license "MIT"
npm config set init.version "0.0.1"

Enter fullscreen mode Exit fullscreen mode

Now our full .npmrc will look like:

prefix=/Users/ajonp/.npm-packages init.author.name=Alex Patterson init.author.email=developer@ajonp.com init.author.url=https://ajonp.com/ init.license=MIT init.version=0.0.1


Enter fullscreen mode Exit fullscreen mode

Executing npm init will produce the following just by hitting enter.

{
    "name": "npm-sample",
    "version": "0.0.1",
    "description": "Sample NPM",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "Alex Patterson <developer@ajonp.com> (https://ajonp.com/)",
    "license": "MIT"
}

Enter fullscreen mode Exit fullscreen mode

Setting NPM registry

At work we have a VSTS (aka Visual Studio aka DevOps) private npm registry so it becomes important to use npm config set registry

https://<company>.pkgs.visualstudio.com/_packaging/<company>/npm/registry/


Enter fullscreen mode Exit fullscreen mode

Which will result in updating .npmrc with

registry=https://<company>.pkgs.visualstudio.com/_packaging/<company>/npm/registry/


Enter fullscreen mode Exit fullscreen mode

There is a great medium article on setting up VSTS npm.

Setting NPM loglevel

Probably my favorite setting of all is npm config set loglevel="warn", this allows me to see any output and only the warnings. There are several different levels in the npm docs, you execute any of them by running something like below:

npm i -g ionic -s --silent: --loglevel silent -q, --quiet: --loglevel warn -d: --loglevel info -dd, --verbose: --loglevel verbose -ddd: --loglevel silly


Enter fullscreen mode Exit fullscreen mode

Resources, Links & Official Documentation

Related CodingCat.dev Tutorials & Podcast Episodes

Frequently Asked Questions (FAQ)

What are the main takeaways from NPM Tips and Tricks?

Adding NPM Globally, setting default values for packages, and stopping the dreaded sudo calls. NPM Tips and Tricks - Installing NodeJs - .npmrc config: πŸŽ“ Lesson: https://ajonp.com/lessons/3-npm/ Ever wonder why you get EACCES without npm sudo? We can fix that and also provide other quick tips. ~~

Please note in order for you to run commands, you **must** update your .bash_profile

Then you can restart Terminal by either closing and restarting, or, In today's video, we'll be covering quick tips with NPM. After installing Node and NPM freshly, you'll typically try to install global package and get this EA access failure. We'll want to correct this. NPM comes with a nice configuration file called {dot} NPMRC that you can locate in your home folder. Currently, mine's empty. What we're going to do is add this line having a prefix home {dot} NPM... Throughout this post, Alex Patterson breaks down real-world implementation details for Node.js, DevOps, JavaScript so developers can apply these patterns directly in production.

How do you implement Node.js best practices in production?

When adopting Node.js and DevOps in a production stack, start by validating your configuration against the Node.js API Reference (Event loop, streams, native fetch, and server-side JavaScript). Focus on incremental adoption, automated testing, and observability so your team can measure performance and developer velocity improvements at each stage.

Where can I watch the full NPM Tips and Tricks walkthrough and find related code resources?

You can watch the complete video session for NPM Tips and Tricks on the CodingCat.dev YouTube channel. For a deeper dive into Node.js, check out our related guide: Speedrun Your App Onto Fly.io.

Top comments (0)