NextJS + Heroku:环境变量未加载
NextJS + Heroku: env variables not loading
我正在使用 NextJS 和 Heroku。
在索引处 - 首先加载 returns 我在 getInitialProps 中获取的数据,但在常规函数中我收到一条错误消息,因为缺少环境变量。
当我转到另一个页面时,我得到了同样的错误,但是当我刷新时,我可以看到在 getInitialProps 获取的数据。但是,在常规函数中,我再次收到缺少环境变量的错误。
本地有效。
我试过 dotenv-webpack 但它没有帮助。
我在 Heroku 中添加了配置变量。
有什么想法吗?
这是我的 next.config.js 文件:
const { parsed: localEnv } = require('dotenv').config()
const webpack = require('webpack')
const Dotenv = require('dotenv-webpack')
const path = require('path')
module.exports = {
//target: 'serverless',
webpack(config) {
config.plugins.push(new webpack.EnvironmentPlugin(localEnv))
config.node = {fs: "empty"};
config.plugins = config.plugins || []
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true
})
]
return config
},
publicRuntimeConfig: {
ADDRESS: process.env.ADDRESS,
API_TOKEN: process.env.API_TOKEN,
INFURA_API_KEY: process.env.INFURA_API_KEY
}
}
在 next.js github 页面中得到了答案:https://github.com/zeit/next.js/issues/6533
我尝试了几种不同的方法来解决冲突。
使用 dotenv-webpack
设置环境变量对我不起作用。起作用的是在 next.config.js
中设置 env
,如下所示:
const { parsed: localEnv } = require('dotenv').config()
const webpack = require('webpack');
const path = require('path')
module.exports = {
webpack(config) {
config.plugins.push(new webpack.EnvironmentPlugin(localEnv))
config.node = {fs: "empty"};
config.plugins = config.plugins || []
config.plugins = [
...config.plugins,
]
return config
},
env: {
ADDRESS: '0xd6F75293ec795',
API_TOKEN: 'YUBKzlbA2eFmNbkzk',
INFURA_API_KEY: '97eb10aac61799f9e865',
MNEMONIC: 'my not so secret for testing password',
}
}
以下是对我有用的(next.config.js):
if (process.env.NODE_ENV === 'development') {
require('dotenv').config()
}
module.exports = {
env: {
API_URL: process.env.API_URL,
}
}
这使得 API_URL 在开发和生产环境的客户端和服务器上都可用。
我正在使用 NextJS 和 Heroku。
在索引处 - 首先加载 returns 我在 getInitialProps 中获取的数据,但在常规函数中我收到一条错误消息,因为缺少环境变量。
当我转到另一个页面时,我得到了同样的错误,但是当我刷新时,我可以看到在 getInitialProps 获取的数据。但是,在常规函数中,我再次收到缺少环境变量的错误。
本地有效。 我试过 dotenv-webpack 但它没有帮助。 我在 Heroku 中添加了配置变量。
有什么想法吗?
这是我的 next.config.js 文件:
const { parsed: localEnv } = require('dotenv').config()
const webpack = require('webpack')
const Dotenv = require('dotenv-webpack')
const path = require('path')
module.exports = {
//target: 'serverless',
webpack(config) {
config.plugins.push(new webpack.EnvironmentPlugin(localEnv))
config.node = {fs: "empty"};
config.plugins = config.plugins || []
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true
})
]
return config
},
publicRuntimeConfig: {
ADDRESS: process.env.ADDRESS,
API_TOKEN: process.env.API_TOKEN,
INFURA_API_KEY: process.env.INFURA_API_KEY
}
}
在 next.js github 页面中得到了答案:https://github.com/zeit/next.js/issues/6533
我尝试了几种不同的方法来解决冲突。
使用 dotenv-webpack
设置环境变量对我不起作用。起作用的是在 next.config.js
中设置 env
,如下所示:
const { parsed: localEnv } = require('dotenv').config()
const webpack = require('webpack');
const path = require('path')
module.exports = {
webpack(config) {
config.plugins.push(new webpack.EnvironmentPlugin(localEnv))
config.node = {fs: "empty"};
config.plugins = config.plugins || []
config.plugins = [
...config.plugins,
]
return config
},
env: {
ADDRESS: '0xd6F75293ec795',
API_TOKEN: 'YUBKzlbA2eFmNbkzk',
INFURA_API_KEY: '97eb10aac61799f9e865',
MNEMONIC: 'my not so secret for testing password',
}
}
以下是对我有用的(next.config.js):
if (process.env.NODE_ENV === 'development') {
require('dotenv').config()
}
module.exports = {
env: {
API_URL: process.env.API_URL,
}
}
这使得 API_URL 在开发和生产环境的客户端和服务器上都可用。