18
loading...
This website collects cookies to deliver better user experience
.env
file can be created suce as the following:.env.development
- when running next dev
.env.production
- when running next start
.env.local
- will override .env
, .env.development
, and .env.production
.env.test
- when running jest
OR cypress
VARIABLE_NAME=VALUE
proccess.env
console.log(process.env.VARIABLE_NAME);
.env*
file will be available on the server-side. Including the second typeDB_HOST=secret_host
DB_USERNAME=username
DB_PASSWORD=password
getStaticProps
, getServerSideProps
, or in /api
export function getStaticProps() {
connectToDatabase({
host: process.env.DB_HOST,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
})
}
NEXT_PULIC_
.NEXT_PUBLIC_GOOGLE_ANALYTICS=abcde12345
NEXT_PUBLIC_NOT_SO_SECRET_URL=https://example.com
// _document.tsx
<Head>
<script
async
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}`}
/>
</Head>
useEffect(() => {
fetch(`${process.env.NEXT_PUBLIC_NOT_SO_SECRET_URL}`)
// ...
})
Although you can still access a browser exposed variable in your server-side code, it will not make sense to do it.
Make sure any sensitive information should not be committed in the repo.