env变量在中不会更改,下一个文件夹nextjs生产

aiazj4mn  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(148)

我使用Netxjs构建了一个全栈应用程序,我从它创建了一个构建版本,并在Cpanel上上传了.next文件夹。我创建了一个node app:
我的节点应用控制台截图:

我在节点env变量和next.config.js中添加了env变量:
next.config.js编码:

/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: false,
  env: {
    dbName: "my_databaseName",
    dbUserName: "my_databaseName",
    dbPassword: "my_databasepassword",
    dbHost: "localhost",
    baseUrl: "https://my-website.com",
    NEXTAUTH_URL: "https://my-website.com",
  },
};

module.exports = nextConfig;

但在日志文件中,我得到了错误:FetchError: invalid json response body at http://localhost:3000/api/home reason: Unexpected token < in JSON at position 0
当我在getServerSideProps函数中检查.next/server/pages/index.js文件时:

const response = await fetch(`${"http:localhost:3000"}/api/home`);

但应该是:

const response = await fetch(`${process.env.baseUrl}/api/home`);

因为这个我得到了500错误的网页上我有HTTP请求

piah890a

piah890a1#

首先,请使用.env文件,而不是在next config文件中添加vars。
其次,当你想部署下一个应用程序时,你必须ignore这些文件夹和文件,然后你必须在主机上构建你的应用程序。
.nextnode_modulespackage-lock.json.env.local
上传.next文件夹将需要很长时间,也可能会崩溃您的应用程序.

相关问题