Next.js Discord

Discord Forum

Not Working Using An `env var` Within a `.npmrc` File

Answered
Donald Louch posted this in #help-forum
Open in Discord
Avatar
Hello,

I am trying to setup Hugeicons' new NPM Token verification method and can get the package installed when passing the token directly into the npmrc file! However, I want to pass this though an enviorment variable to be more secure! But when I do, I am unable to install the package as it can't find the token.

# .env (tried .env \ .env.local \ .env.development)

// I've tried the following
NEXT_PUBLIC_NPM_TOKEN=thetoken
NPM_TOKEN=thetoken
NEXT_PUBLIC_NPM_TOKEN="thetoken"
NPM_TOKEN="thetoken"
NEXT_PUBLIC_NPM_TOKEN = "thetoken"
NPM_TOKEN = "thetoken"


# .npmrc

; I've tried the following
//registry.npmjs.org/:_authToken=$NEXT_PUBLIC_NPM_TOKEN
//registry.npmjs.org/:_authToken=$NPM_TOKEN
//registry.npmjs.org/:_authToken=${NEXT_PUBLIC_NPM_TOKEN}
//registry.npmjs.org/:_authToken=${NPM_TOKEN}


am I missing something?! Do I need to do anything within the next.config.js config file?!

Thanks,
Donald Louch
Answered by Donald Louch
I think I have found the solution? There is no need to create a npmrc file for Vercel! You just need to add a NPM_TOKEN env var into the dashboard within the Vercel project and Vercel will add the appropriate npmrc file 🎉

https://vercel.com/guides/using-private-dependencies-with-vercel
View full answer

24 Replies

Avatar
this is probably since
env variables aren'tloaded
if running NPM_TOKEN="thetoken" npm i works that's the cause
Avatar
Do you know how I can fix this?!
Avatar
@Donald Louch did you try this
Avatar
So would it be

// .env
NPM_TOKEN="thetoken" npm i


// .npmrc
//registry.npmjs.org/:_authToken=$NPM_TOKEN


If so, then yes I have tried that and still no luck!
Avatar
no
just try running
in the terminal
NPM_TOKEN="thetoken" npm i
replacing thetoken with your token
Avatar
I just tried that and it isn't working either!
Avatar
wait
you're supposed to do it like registry.npmjs.org/:_authToken={$NPM_TOKEN}
with the parenthesis
and then you add the token to your bash/zshrc
since it doesn't pick up from .env iirc but i might be wrong
Avatar
Okay, do you know if I can have that added to vercel for deployment?!
Avatar
Vercel env's likely gonna work
I'm not sure
try it and see
that was the wrong emoji i thought there was one about tryitandsee
Avatar
Update: did not work with Vercel 😅
Avatar
I think I have found the solution? There is no need to create a npmrc file for Vercel! You just need to add a NPM_TOKEN env var into the dashboard within the Vercel project and Vercel will add the appropriate npmrc file 🎉

https://vercel.com/guides/using-private-dependencies-with-vercel
Answer