下面是我的package.json,我正在尝试加载staging变量。在我的根目录中,我有三个环境文件,即.env.local,.env.production,. env. staging。但只有.env.local,.env.production正在加载。
{
"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.staging文件。下面是日志。
> 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
1条答案
按热度按时间wlp8pajw1#
从您提供的日志中可以看出,您的环境变量仍然没有从.env.staging文件中加载。
您提到您使用env-cmd从文件中加载环境变量。您可以尝试的一件事是直接在构建阶段脚本中运行env-cmd命令,如下所示:
enter code here
“scripts”:{“build-staging”:“env-cmd -f .env.暂存下一个生成”}这样可以确保在构建过程中正确加载.env.staging文件中的环境变量。
或者,您可以尝试在运行下一个构建命令之前将NODE_ENV环境变量显式设置为“staging”,如下所示:
enter code here
“scripts”:{“build-staging”:“NODE_ENV=暂存下一个构建”}确保删除可能覆盖此设置的任何其他NODE_ENV环境变量赋值。
另外,请确保.env.staging文件位于正确的目录中,并且包含正确的环境变量。