22

I use mac and I installed gatsby-cli by:

npm install --global gatsby-cli

and 'npm root -g' command prints:

/usr/local/lib/node_modules/npm/lib/node_modules

npm successfully installed gatsby but, I cannot run gatsby command (gatsby --version, gatsby --help) Whenever I type and run "gatsby --version" it prints:

-bash: gatsby: command not found

Is there any way I can fix this problem??

8
  • 1
    could you please try gatsby info instead of checking version ?
    – Amruth
    Sep 18, 2018 at 4:15
  • gatsby info >>>> -bash: gatsby: command not found
    – leonkong
    Sep 18, 2018 at 4:24
  • have you reopened the terminal ?
    – Amruth
    Sep 18, 2018 at 4:31
  • I use bash terminal. I don't understand what you are meaning by "responded the terminal"
    – leonkong
    Sep 18, 2018 at 4:42
  • 1
    check my updated ans
    – Amruth
    Sep 18, 2018 at 5:02

4 Answers 4

43

you have to update your config. You may have to follow few steps

Set Config

remove existing config

npm config delete prefix 

set new config

npm config set prefix /usr/local

Install gatsby

npm i -g gatsby-cli

now check version

gatsby --version
5
  • I did it.. but it still returns >>>-bash: gatsby: command not found
    – leonkong
    Sep 18, 2018 at 4:44
  • oh! it helps!! now i can get result for gatsby --version Thankyou a lot!!!
    – leonkong
    Sep 18, 2018 at 5:22
  • Nice. Can you elaborate what the issue is? Why does the solution work? Oct 17, 2020 at 23:36
  • @XaverFleer npm config prefix might be pointing to incorrect location. so we are removing it and setting new location.
    – Amruth
    Oct 19, 2020 at 8:08
  • Great. It worked on my end, now set at 3.0 (I had installed gatsby locally before in the project folder) Gatsby CLI version: 3.0.0 Gatsby version: 3.0.0
    – cdsaenz
    Mar 3, 2021 at 12:51
5

Instead of trying to install globally, you can use npx

 npx gatsby new my_dir_name

Then run:

npx gatsby develop

Then:

npx gatsby build

And then you can run them using

npm run develop
npm run build
2
  • this seems like the cleanest solution IMO
    – Cole
    Apr 29, 2022 at 19:56
  • This solution worked for me. For some unknown reason, I could not run then npm global command. You save my day. thank you. Oct 29, 2022 at 10:34
2

I came across the same issue. npx gatsby -v worked. Hope this helps somebody. Reference: https://github.com/gatsbyjs/gatsby/issues/15276#issuecomment-507340096

1
  • This doesn't actually mean it is installed locally. "npx" will just pull from the NPM global repository to grab it temporarily and then run the command with it, but it isn't installed locally which means anytime you want to do anything with gatsby you have to start it with "npx". github.com/npm/npx
    – BlueCaret
    Jan 7, 2021 at 15:12
2

In my case i was missing sudo on install:

sudo npm i -g gatsby-cli

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.