简体   繁体   English

Next.js 独立不读取环境文件

[英]Next.js standalone not reading env file

I have a question about my Next.js project.我对我的 Next.js 项目有疑问。

I have configured my project to be build over the standalone mode for the deployment我已将我的项目配置为通过独立模式构建以进行部署

experimental: {
    outputStandalone: true
}

Using this, it generates me a standalone folder with a server.js like expected.使用它,它会为我生成一个带有预期的server.js的独立文件夹。

The main issue is that I am using an env variable in my sources, called NEXT_PUBLIC_API_BASE_URL主要问题是我在源代码中使用了一个环境变量,称为NEXT_PUBLIC_API_BASE_URL

When I start my project in development mode (using next serve ), it works fine.当我以开发模式(使用next serve )启动我的项目时,它工作正常。

But when I start the standalone generated file (using node server.js ) it does not work.但是当我启动独立生成的文件(使用node server.js )它不起作用。

It seems that the file is loaded on the "server side", when I console.log its value in the .next/standalone/server/pages/_app.js it shows the right value in the node console.似乎该文件是在“服务器端”加载的,当我在.next/standalone/server/pages/_app.jsconsole.log它的值时,它在节点控制台中显示了正确的值。

But it looks like next is using files under .next/static/chunks/pages/ and an other app.js that does not seem to access process.env (on browser side).但看起来下一个正在使用.next/static/chunks/pages/下的文件和另一个似乎无法访问 process.env 的 app.js(在浏览器端)。

I thought that prefixing my env var with NEXT_PUBLIC was meant to work this way, it seems not.我认为在我的 env var 前面加上NEXT_PUBLIC是为了以这种方式工作,但似乎不是。

Any idea on how it works there?关于它如何在那里工作的任何想法?

On your build server, create the file name ".env" and put all of your environments.在您的构建服务器上,创建文件名“.env”并放置所有环境。

NEXT_PUBLIC_API_BASE_URL=https://api.example.com

please refer Docs请参考文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .env 文件访问令牌适用于 API 而不是 Next.js 中的另一个 - .env file access token working for API but not another in Next.js 在 Next.js 中,有没有办法将 .env 文件的位置更改为应用程序文件夹之外的位置? - In Next.js, Is there a way to change the location of the .env file to be outside of the app's folder? Next.js 上基于 NODE_ENV 的条件 url - Conditional url based on NODE_ENV on Next.js Next.js 将 NODE_ENV 传递给客户端 - Next.js pass NODE_ENV to client 无法从 Next.js 中的 React 组件访问 env - Can't reach env from React Component in Next.js Next.js 错误:无法读取未定义的属性(读取“集合”) - Next.js Error: Cannot read properties of undefined (reading 'collection') 类型错误:无法读取 Next.js 上未定义的属性(读取“调用”) - TypeError: Cannot read properties of undefined (reading 'call') on Next.js Next.js - 类型错误:无法读取 null 的属性(读取“useMemo”) - Next.js - TypeError: Cannot read properties of null (reading 'useMemo') Next.js | TypeError:无法读取未定义的属性(读取“秘密”) - Next.js | TypeError: Cannot read properties of undefined (reading 'secret') TypeError:无法读取 Next.js 中 null 的属性(读取“默认”) - TypeError: Cannot read properties of null (reading 'default') in Next.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM