【发布时间】:2020-03-21 22:20:05
【问题描述】:
有一个 Next.js 项目。
这是我在本指南中使用的 next.config.js 文件:https://dev.to/tesh254/environment-variables-from-env-file-in-nextjs-570b
module.exports = withCSS(withSass({
webpack: (config) => {
config.plugins = config.plugins || []
config.module.rules.push({
test: /\.svg$/,
use: ['@svgr/webpack', {
loader: 'url-loader',
options: {
limit: 100000,
name: '[name].[ext]'
}}],
});
config.plugins = [
...config.plugins,
// Read the .env file
new Dotenv({
path: path.join(__dirname, '.env'),
systemvars: true
})
]
const env = Object.keys(process.env).reduce((acc, curr) => {
acc[`process.env.${curr}`] = JSON.stringify(process.env[curr]);
return acc;
}, {});
// Fixes npm packages that depend on `fs` module
config.node = {
fs: 'empty'
}
/** Allows you to create global constants which can be configured
* at compile time, which in our case is our environment variables
*/
config.plugins.push(new webpack.DefinePlugin(env));
return config
}
}),
);
我有一个 .env 文件,其中包含我需要的值。它在本地主机上运行时有效。
在我的 Kubernetes 环境中,在我可以修改的部署文件中,我设置了相同的环境变量。但是当我尝试识别它们时,它们会显示为未定义,因此我的应用程序无法运行。
我这样称呼它:
process.env.SOME_VARIABLE
在本地工作。
有没有人在部署时有在 Next.js 上创建环境变量功能的经验?不像后端服务那么简单。 :(
编辑: 这就是环境变量部分的样子。
编辑 2: 完整的部署文件,编辑删除了一些细节
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "38"
creationTimestamp: xx
generation: 40
labels:
app: appname
name: appname
namespace: development
resourceVersion: xx
selfLink: /apis/extensions/v1beta1/namespaces/development/deployments/appname
uid: xxx
spec:
progressDeadlineSeconds: xx
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: appname
tier: sometier
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: appname
tier: sometier
spec:
containers:
- env:
- name: NODE_ENV
value: development
- name: PORT
value: "3000"
- name: SOME_VAR
value: xxx
- name: SOME_VAR
value: xxxx
image: someimage
imagePullPolicy: Always
name: appname
readinessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 3000
scheme: HTTP
initialDelaySeconds: 5
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
resources:
requests:
cpu: 100m
memory: 100Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 1
conditions:
- lastTransitionTime: xxx
lastUpdateTime: xxxx
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
observedGeneration: 40
readyReplicas: 1
replicas: 1
updatedReplicas: 1
【问题讨论】:
-
您的项目中的 .env 文件在哪里?此外,可能值得查看该文件的权限。
-
.env 与 package.json 等一起存在于根目录中(在 /src 目录之外)
-
对于 Kubernetes,我执行“kubectl edit deploy project_name”,它允许我修改已部署产品的配置,还可以像我一样添加环境变量。此方法适用于后端服务,但浏览器内容不同。
-
你能分享你的部署文件吗?
-
我已将环境变量部分的示例添加到 OP
标签: kubernetes environment-variables next.js