env-cmd error failed to locate ./.env file in gatsby?

后端 未结 12 712
鱼传尺愫
鱼传尺愫 2020-12-15 05:33

I have a file name .env.development in the root folder. I had install env-cmd as dev dependencies
when I start the server

  > npm run develop
         


        
12条回答
  •  一整个雨季
    2020-12-15 06:11

    Ran into the same issue, here's a snippet of gatsby-config.js I added to make the ".env.development" file visible to gatsby. (Not sure if this is the only way, node/Gatsby experts please chime in)

    
    let activeEnv =
      process.env.GATSBY_ACTIVE_ENV || process.env.NODE_ENV || "development"
    
    console.log('Using environment config: ${activeEnv}')
    
    require("dotenv").config({
      path: `.env.${process.env.NODE_ENV}`,
    })
    

    Based on this gatsby-config.js, here is the develop script in package.json (unchanged) -

    
    ...
    "develop": "gatsby develop",
    ...
    

    Finally, starting the gatsby app using

    npm run develop
    , the logs mentions playground being available -

    
    You can now view gatsby-starter-hello-world in the
    ⠀
      http://localhost:8000/
    ⠀
    View the GraphQL Playground, an in-browser IDE, to
    ⠀
      http://localhost:8000/___graphql
    

提交回复
热议问题