简体   繁体   English

使用 cPanel 的 NodeJs 中的环境变量

[英]Environment variables in NodeJs using cPanel

So I'm using cPanel with Setup Node.js App plugin for a Next.js app.因此,我将 cPanel 与 Setup Node.js App 插件一起用于 Next.js 应用程序。 (don't asky why cPanel) (不要问为什么是cPanel)

Everything is working as expected in development, except for environment variables in production, I set up them manually from the cPanel interface, I restarted/stopped the app, logging the process.env on the server and I don't see the env variables there (not to say when trying to call them they are undefined).一切都在开发中按预期工作,除了生产中的环境变量,我从 cPanel 界面手动设置它们,我重新启动/停止应用程序,在服务器上记录 process.env,我看不到那里的 env 变量(并不是说在尝试调用它们时它们是未定义的)。

When doing res.json(JSON.stringify(process.env));当做res.json(JSON.stringify(process.env)); i get a bunch of variables except for the one I manually wrote in cPanel variables interface.除了我在 cPanel 变量界面中手动编写的变量之外,我得到了一堆变量。

It is important for me to store these variables as secret key because they are API credentials.将这些变量存储为密钥对我来说很重要,因为它们是 API 凭据。

Anyone know what I might have misconfigured or had this problem?有谁知道我可能配置错误或遇到了这个问题?

Never mind, found the answer, apparenlty was a Next.js misconfiguration.没关系,找到答案,显然是 Next.js 配置错误。 I had to add the following lines of code inside next.config.js in order to read env variables on build version.为了读取构建版本的环境变量,我必须在next.config.js中添加以下代码行。

require('dotenv').config();

module.exports = {
  env: {
    EMAIL_NAME: process.env.EMAIL_NAME,
    EMAIL_PASSWORD: process.env.EMAIL_PASSWORD,
    GETRESPONSE_API_KEY: process.env.GETRESPONSE_API_KEY
  }
};

Where EMAIL_NAME, EMAIL_PASSWORD, GETRESPONSE_API_KEY were the variables defined by me on cPanel interface其中 EMAIL_NAME、EMAIL_PASSWORD、GETRESPONSE_API_KEY 是我在 cPanel 界面上定义的变量

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM