1
I’m trying to publish a npm package with a file .env
using the package dotenv.
When I test locally using the process.env.MINHA_PROPRIEDADE
, it works normally though, when I try to use installing the package published globally, it doesn’t work.
I have tried to change the content of .gitignore
not to include . env, but this did not solve the problem.
Is it possible for me to use an environment variable when publishing a package on NPM? If so, where am I missing?
The code I’m using is as follows::
require('dotenv').config()
function main() {
console.log(`A chave é ${process.env.MINHA_VARIAVEL}`)
}
main()
And my file .env
has the following:
MINHA_VARIAVEL=TESTE
Basically, this is a package to test how to do this...
What happens when I run locally is the printing on the console of the phrase "The key is TEST", whereas when I publish and install globally, the return I get is "The key is Undefined".
You have the code example to clarify what you are doing?
– novic
@Virgilionovic I edited with an example code
– Felipe Avelar
It won’t be that your program is trying to access . env from another location when it publishes, because in this case the package is installed in the modules. I’m just guessing, not stating ;)
– Guilherme Nascimento
@Guilhermenascimento was just that, I had to change the config and add the parameter
{path: path.resolve(__dirname, '.env')}
to solve the problem. Put this as an answer for me to accept (:– Felipe Avelar