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

后端 未结 12 709
鱼传尺愫
鱼传尺愫 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:10

    Step 1: First run the command: npm install --save-dev env-cmd@10.1.0

    Step 2: Replace env-cmd .env-development gatsby develop with ./node_modules/.bin/env-cmd -f ./.env.development gatsby develop

    0 讨论(0)
  • 2020-12-15 06:10

    Thanks, everyone. This solves it for me

    "develop": "env-cmd --file .env.development --fallback gatsby develop",
    

    and pass in this value .env.development file GATSBY_GRAPHQL_IDE=playground.

    In case, you want to understand how to set it up better, You can check out this article on CSS Tricks by Adebiyi Adedotun

    0 讨论(0)
  • 2020-12-15 06:11

    use the -f flag and make sure the path to your .env.development file is correct.

    "develop": "env-cmd -f ./.env.development gatsby develop"
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-15 06:11

    Adding the let snippet to the gatsby-config.js file did the trick for me! And then starting up with gatsby develop Tnx! Great help!

    0 讨论(0)
  • 2020-12-15 06:14

    This has been updated in the latest version of env-cmd, if you are using version <9.0.0 then it will work perfectly but with version >9.0.0 the default environment file it will look for is .env

    use env-cmd -f .env.development gatsby develop instead, here -f is provided for custom file name.

    0 讨论(0)
提交回复
热议问题