TL;DR
This solution works for Node v16.13.0 and NPM v8.1.3
Introduction
I don’t always hate working using NPM unless it starts downloading tons of node_modules and giving not so verbose logs. Today, my colleague and I found an NPM login bug that’s not so interesting and hard to debug. It takes me about an hour to find out what is the root cause.
In this article, I will write the solution just in case you meet the same issue.
Current Condition
In my current company, we use Nexus3 as our private repository for the NPM module. I also use Node v12.22.4 and NPM v8.1.3 for working on my local machine. To login into our Nexus repository, I use .npmrc with this format:
| |
And we’re happy with it. No issue.
The Issue
The issue comes when my colleague wants to try using Node v16.13.0. When the npm i @myorg/utils command is executed, it starts saying 401.
| |
Finding a Solution
I wonder if it’s the Nexus 3 is not compatible with the Node v16 or what. So I tried to re-login to the Nexus repository.
| |
Okay, now we’re logged in. But as soon as I check using npm whoami, it said 401 again.
| |
Then I check my .npmrc file content, turns out Node v16 has a different format of .npmrc. Here is my current .npmrc file content:
| |
What a strange format. It doesn’t reflect the scope, also I am still confused with the _authToken format itself. Of course, as a good developer, we need a fast hand for searching every keyword on google for our bugs. And then, I found this comment by @apottere. So, I tried to rewrite my .npmrc file content manually. This is my current .npmrc file content:
| |
Seems promising, so I tried again to execute npm i @myorg/utils again. And it failed.
| |
Yep, not found. I thought it was because the scope isn’t reflected in the .npmrc file.
The Solution
So I tried using the old way, rewriting the content manually. This is my current and final .npmrc file content:
| |
Lastly, I tried again the npm i @myorg/utils command and it worked.
| |
Conclusion
So, It was the npm login command all along. I still can’t find the whole documentation about the new .npmrc format, I might miss the docs, or maybe there isn’t one. If you have the same issue, I hope you find this article and can fix the issue.
Thank you for reading!