Sharing .npmrc between local build and production deploy

Hi,

I am using .npmrc to authenticate for a private Github package.

In order for Netlify to access my auth token, I pass it into .npmrc like this:

@netlify:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${NPM_TOKEN}

However, when I build locally, I get the error

"Failed to replace env in config: ${NPM_TOKEN}"

Can anyone offer some advice on how to access environment variables locally using process.env and through Netlify using ${SOME_NETLIFY_VARIABLE} at the same time? Currently I have to manually edit the file between environments.

Thanks

If you use Netlify Dev, you should be able to use process.env.

1 Like

Thanks @hrishikesh , I’ll definitely look into that.

But is there a way to do it without using Netlify Dev? I’m imagining there’s some sort of logic saying something like “if local, use the auth token on my machine, else if using Netlify, use the env variable”

Well, in most Static Site Generators there’s a way to differentiate between development and production builds, but since this part is not being managed by a SSG, I’m not sure if conditional logic is possible here.

Maybe someone else can chip in.

1 Like

maybe you could utilize context for this?

Contexts would be useful if the variables are different between production and development, if I’m not wrong. But, in this case, the variables have the same value, it’s just a difference of how to access them in the code.