【发布时间】:2021-01-14 09:44:11
【问题描述】:
下面是 package.json 文件中的代码
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
},
下面是我的 next.config.js 文件,这里的 console.log 总是 undefined
require("dotenv").config();
console.log(process.env.BASE_URL)
module.exports = {
env: {
API_BASE_URL: process.env.BASE_URL
},
reactStrictMode: true,
}
这是在 .env.development 中
BASE_URL: 'http://localhost:3000'
当我运行 npm run dev 命令时, 它打印在终端“Loaded env from /var/www/html/next/next-SC/.env.development”
那么,为什么控制台总是打印 undefined。
我正在使用下一个 js 版本“10.0.4”
【问题讨论】:
-
因为文件 nextjs 配置会在加载 env 文件之前加载。
-
不应该将
.env.development中的环境变量设置为BASE_URL=http://localhost:3000? -
在我的 Ubuntu 服务器中,我只是在本地和服务器中设置了不同的环境变量。它们是自动获取的。只需要记住,如果要将环境变量公开,则必须添加前缀 NEXT_PUBLIC
标签: environment-variables next.js development-environment production-environment