If this code is executed in a browser, window is available as a global object. However, if this code is executed in a node environment (server side), window will be undefined.
Here's one way to handle this if your code will execute both server-side (node environment) and client-side (browser environment):
if (typeof window !== 'undefined') {
configs = {
//firebase configuration
apiKey: window._env_.API_KEY,
projectId: window._env_.PROJECT_ID,
messagingSenderId: window._env_.MESSAGING_SENDER_ID,
backendURL: window._env_.BACKEND_URL
}
} else {
// handle server-side logic here
}
If there's no need for this to execute in the browser, it would be simplest to just use process.env instead of setting these variables on the window. If you do need these variables in both places (and they're coming from process.env), this might be another solution:
const env = typeof window === 'undefined' ? process.env : window._env_;
export const configs = {
//firebase configuration
apiKey: env.API_KEY,
projectId: env.PROJECT_ID,
messagingSenderId: env.MESSAGING_SENDER_ID,
backendURL: env.BACKEND_URL
}
xxxxin place of actualAPI_KEYvalue just to be safe.