You want to build your site on Netlify, but you have some private npm modules served from npmjs.org. So how can you access them during build?
The most straightforward way to do this is two steps:
- Find your token inside the
~/.npmrc file in your home folder and set it as an
NPM_TOKENenvironment variable through Netlify’s admin UI. This will be on the Build & Deploy settings page of your site configuration, in the section called “Build Environment Variables”:
- Create a
/.npmrcfile for your project like this (and commit it to Git):
That should let us automatically pull the module from npm using your token, and your token does NOT need to be committed to your repo. If you’d rather have it there than in Netlify for some reason, that’s also functional If you store it at Netlify the only people who can see it are people whom you’ve invited to your Netlify account; if you store it in your repo, it’s instead visible to everyone who can see your repo.
Some other related information on the topic can be found in the following threads:
- This thread in the npm community talks about how npm uses the environment, which explains why your .env file won’t “just work” in this case.
- If you need to set up access to something OTHER than a module to fetch from npm, you might instead find this post in our community to be useful. It describes accessing secondary repositories during build.