下面是我的包。json和我试图加载暂存变量。在我的根目录中,我有三个环境文件,即。env。地方,.env。生产、.env.staging。但只有。env。地方,.env。
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build-staging": "env-cmd -f .env.staging next build",
"staging": "env-cmd -f .env.staging next start",
"build-prod": "env-cmd -f .env.production next build",
"prod": "env-cmd -f .env.production next start",
"lint": "next lint"
},
"dependencies": {
"@types/node": "18.15.10",
"@types/react": "18.0.30",
"@types/react-dom": "18.0.11",
"eslint": "8.36.0",
"eslint-config-next": "13.2.4",
"next": "13.2.4",
"next-pwa": "^5.6.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"typescript": "5.0.2"
},
"devDependencies": {
"env-cmd": "^10.1.0",
"yarn": "^1.22.19"
}
}
当我运行脚本"npm build-staging"它没有加载。ev。暂存文件。下面是日志。
> env-cmd -f .env.staging next build
info - Loaded env from /Users/nityanand/NextPOC/my-app/.env.local
info - Loaded env from /Users/nityanand/NextPOC/my-app/.env.productioninfo - Linting and checking validity of types
> [PWA] Compile server
> [PWA] Compile client (static)
> [PWA] Auto register service worker with: /Users/nityanand/NextPOC/my-app/node_modules/next-pwa/register.js
> [PWA] Service worker: /Users/nityanand/NextPOC/my-app/public/sw.js
> [PWA] url: /sw.js
> [PWA] scope: /
> [PWA] Compile server
info - Compiled successfully
info - Collecting page data
info - Generating static pages (3/3)
info - Finalizing page optimization
从您提供的日志来看,您的环境变量似乎仍然没有从.env文件加载。暂存文件。
您提到您正在使用env-cmd从文件加载环境变量。您可以尝试的一种方法是在构建分级脚本中直接运行env-cmd命令,如下所示:
enter code here
"scripts": {"build-staging"; "准备下一个构建}
这将确保.env. xml文件中的环境变量。在构建过程中正确加载分段文件。
或者,您可以尝试显式地将NODE_ENV环境变量设置为"staging"在运行下一个构建命令之前,像这样:
enter code here
"scripts": {"build-staging";NODE_ENV=暂存下一次构建"}
确保删除可能覆盖此设置的任何其他NODE_ENV环境变量赋值。
另外,确保.env. xml文件Staging文件在正确的目录中,并且包含正确的环境变量。