Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use @next/env instead of dotenv in Next.js App Examples #300

Closed
josbelluna opened this issue Sep 17, 2024 · 5 comments
Closed

Use @next/env instead of dotenv in Next.js App Examples #300

josbelluna opened this issue Sep 17, 2024 · 5 comments

Comments

@josbelluna
Copy link

Is your feature request related to a problem? Please describe.

No

Feature Description

For NextJS App Examples, use @next/env instead of dotnev to match NextJS environment variables.

There are some issues using dotnev library directly to load NextJS env variables, for example trying to load them from .env.local doesn't work by default.

NextJS now exposes @next/env package that can be used to get the same results as the main process:
https://nextjs.org/docs/app/building-your-application/configuring/environment-variables#loading-environment-variables-with-nextenv

// newrelic.js
const { loadEnvConfig } = require('@next/env')

const projectDir = process.cwd()
const dev = process.env.NODE_ENV !== 'production'
loadEnvConfig(projectDir, dev)
@workato-integration
Copy link

@jsumners-nr
Copy link
Contributor

Thank you for the suggestion. I do not think it is necessary. Our purpose with these examples is to show how to utilize the New Relic Node.js agent with Next.js, not to provide tutorials for Next.js itself. Personally, I am also 100% against adding any sort of process.env.NODE_ENV references. NODE_ENV is a very poor practice; see https://www.youtube.com/watch?v=irRsEfCTTtg

@josbelluna
Copy link
Author

Yes, I agree with not using NODE_ENV for detecting the environment, in fact it is not required to be use it, as it is not the key for getting the environment file. You can perfectly use it as:

const { loadEnvConfig } = require('@next/env')

const projectDir = process.cwd()
loadEnvConfig(projectDir)

Then you don't need to add NODE_OPTIONS='-r dotenv/config --loader newrelic/esm-loader.mjs' or importing dotenv.

I added because the second argument of loadEnvConfig is for detecting if you are on a dev server, which has some unique cases for NextJS, for example if not specified it will read variables for env.production if the keys doesn't exist on env.local and that may not be wanted. You can use whatever to detect you are in a dev server.

I asked because there are some of posts and issues asking about this on other threats, like these:
https://forum.newrelic.com/s/hubtopic/aAX8W0000015AjuWAE/nextjs-13-env-variables-are-undefined
newrelic/newrelic-node-nextjs#143

But none of them seems to work correctly with the same env files configuration NextJS suggest to use, we need to use .env but NextJS supports .env.local, .env.production, and .env.test.

@jsumners-nr
Copy link
Contributor

The newrelic agent is loaded prior to Next.js. We cannot rely on their systems for parsing environment variables.

@jsumners-nr jsumners-nr closed this as not planned Won't fix, can't repro, duplicate, stale Sep 18, 2024
@bizob2828
Copy link
Member

@josbelluna this example is just demonstrating the observability the New Relic Node.js agent provides as well as some examples of common problems our customers hit: injecting browser agent, forwarding logs, handling errors. This is not intended to be a template for the best way to use the Node.js agent with Next.js.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done: Issues recently completed
Development

No branches or pull requests

3 participants