英文:
Contentful with AWS Amplify and Next.js: TypeError: Expected parameter accessToken
问题
我有一个托管在AWS Amplify上的Next.js应用程序。我有一个具有命名导出的静态页面 getStaticProps
,在其中我使用了Contentful,如下所示:
const client = contentful.createClient({
space: process.env.CONTENTFUL_SPACE,
accessToken: process.env.CONTENTFUL_API_KEY,
})
但是,我遇到了这个错误:
TypeError: Expected parameter accessToken
at Module.createClient (/var/task/node_modules/contentful/dist/contentful.node.js:10095:11)
尽管我在AWS Amplify仪表板中添加了CONTENTFUL_SPACE
和CONTENTFUL_API_KEY
环境变量,但我仍然遇到了这个问题。
在使用Vercel时,我没有遇到这个问题。有人知道为什么我在AWS Amplify上添加了环境变量后仍然会遇到这个错误吗?
英文:
I have a Next.js app hosted on AWS Amplify. I have a static page with a named export getStaticProps
where I use contentful, like this:
const client = contentful.createClient({
space: process.env.CONTENTFUL_SPACE,
accessToken: process.env.CONTENTFUL_API_KEY,
})
However, I am getting this error:
TypeError: Expected parameter accessToken
at Module.createClient (/var/task/node_modules/contentful/dist/contentful.node.js:10095:11)
even though I added the CONTENTFUL_SPACE
and CONTENTFUL_API_KEY
environment variables in the AWS Amplify dashboard.
I don't get this issue when using Vercel. Does anyone know why I am getting this error on AWS Amplify even though I added the environment variables in the Amplify dashboard?
答案1
得分: 1
以下是我解决此错误的方法:
在我的 next.config.js
中,我添加了以下内容:
module.exports = {
env: {
CONTENTFUL_SPACE: process.env.CONTENTFUL_SPACE,
CONTENTFUL_API_KEY: process.env.CONTENTFUL_API_KEY,
}
}
这是必要的原因,以及为什么在 AWS Amplify 的 getStaticProps
中无法访问环境变量的原因,是因为环境变量不会传递到 Lambda 函数,而 getStaticProps
是在 Lambda 函数中运行的。
英文:
Here is how I solved this error:
In my next.config.js
, I added:
module.exports = {
env: {
CONTENTFUL_SPACE: process.env.CONTENTFUL_SPACE,
CONTENTFUL_API_KEY: process.env.CONTENTFUL_API_KEY,
}
}
The reason this was necessary and the reason the environment variables weren’t accessible in getStaticProps
on AWS Amplify is because Environment variables are not carried through to Lambda functions which is where getStaticProps
runs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论