为什么“ npm运行开发”工作而“盖茨比开发”抛出错误?

时间:2020-04-27 14:18:50

标签: gatsby

在控制台上运行'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
      }

1 个答案:

答案 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。