Skip to content
Advertisement

Can’t access API-key from .env-file

We are having problem accessing the API-key from our .env file when trying to fetch in our server.js. If we add the API-key manually to the URL in server.js it works, so the problem seems to be the connection between server.js and .env-file. We have npm installed dotenv. In the .env file we have written the key like this: WEATHER_API_KEY = XXXXXXXXXXXX

Does anyone know what we have done wrong?

JavaScript

Advertisement

Answer

You may try this

JavaScript

in place of import dotenv from "dotenv"; and remove the dotenv.config(); call.

Source and explanation: https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import

And update the request URL (which you might have changed for testing purpose) to

JavaScript

Also, try defining your key without any spaces, though this is less likely to be the root cause.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement