'NPM install package from private repository using access token from environment

I want to install an npm package from a private repository from a specific branch. I know it's possible if you explicitly specify the token in the package path, but I don't want to keep any access tokens inside of package.json and so I was wondering if there's a way to do this:

"package-name": "git+https://<github_token>:[email protected]/<user>/<repo>.git"

with <github_token> not directly specified in the code, but for it to be read from the environment (either .npmrc or NPM_TOKEN env variable).

Is this possible?



Solution 1:[1]

So I just figured that I can do replace in my pipeline before the installation step, like this:

sed -i "s/ssh:\/\/[email protected]/git+https:\/\/${NPM_TOKEN}:[email protected]/g" package.json \

in package.json I would have:

    "package-name": "ssh://[email protected]/<user>/<repo>",

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 comonadd