在控制台上运行'gatsby开发'会出现以下错误,但在部署以netlify进行正常工作时,'npm run development'和'gatsby development'一样。有人可以解释为什么吗?
运行gatsby开发时发生错误:
Problems with gatsby-source-contentful plugin options:
spaceId: undefined - "spaceId" is required
accessToken: undefined - "accessToken" is required
即使在.env.development中定义了令牌,并且设置了插件选项:
resolve: 'gatsby-source-contentful',
options: {
spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN
}
答案 0 :(得分:0)
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
})
module.exports = {
siteMetadata: { ... },
plugins: [
...
`gatsby-transformer-remark`,
{
resolve: `gatsby-source-contentful`,
options: {
spaceId: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
},
},
...
]
}
请确保您通过添加这些代码来指定.env路径。错误是因为无法找到spaceId和accessToken。