2

I wanted to create a react project and when I executed the command it said zsh: command not found: npx Then I tried the ls command and it said zsh: command not found: ls.

After setting the export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" command both the ls and npx create-react-app command worked fine and when close the terminal and reopen again, the same command not found error shows. Is there any permenent fix without setting export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" command

Minsaf
  • 272
  • 1
  • 5
  • 14
  • See the section _STARTUP/SHUTDOWN FILES_ in the _zshall_ man-page. It tells you where you have to place definitions to have them automatically available. – user1934428 Feb 18 '22 at 09:13
  • Note also that setting the PATH **somewhere** in these files is pointless, if you later on overwrite it. Therefore I would create a new shell with the `-x` option enabled, and analyze the output you get. You will see all places where your PATH is modified. – user1934428 Feb 18 '22 at 09:20
  • Note that the default PATH is fine, so to get this problem you or some software you ran had to add something to your dotfiles to create a PATH that _isn't_ fine. Finding and removing that thing is the best approach here; you don't need to add something that sets a correct path if you can just remove whatever is setting an _incorrect_ one. – Charles Duffy May 31 '23 at 14:55

2 Answers2

5

For adding the variables to the path you need to add it to zshrc file for making that variable available locally.

The way you have used will only work until you use it in the same terminal window path only.

To solve the problem, follow these steps:

  1. Goto you home directory

  2. Simultaneously press cmd + shift + (.) Note:the last key is the key of dot

  3. On following step 2, new hidden files will appear in home directory, look for (.zshrc) file and open it using any text editor.

  4. Add your path variable in it, save and then close it.

Example: export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"

  1. Open terminal and run the command: "echo $PATH" and see if your added variable is present in the output shown by terminal.

  2. If yes, You are now ready to go to use it from anywhere in terminal now.

  • the path is already there. still not working – Minsaf Feb 18 '22 at 06:02
  • `/opt/homebrew/bin:/usr/lo/Users/minsafmohamed/flutter/bin` This is what printing when run the command echo $PATH – Minsaf Feb 18 '22 at 06:05
  • I guess you mean `~/.zshrc`. Note that this file is only processed for **interactive** shells. Sadly, the OP does not say what kind of shell is running, when he gets this error message. – user1934428 Feb 18 '22 at 09:18
  • 1
    If you can not find ~/.zshrc, check this answer https://superuser.com/a/886135/1653602 – Salma Gomaa Apr 27 '22 at 11:16
1

This is what worked for me on macOS Monterey,

Although I added the path to ./zshrc and sourced the file, after reopening the terminal the PATH was not exported

I followed these steps to solve this

  1. Created .zprofile with touch .zprofile at the home directory. If the file already exists use that.
  2. Add the required path to this file using vim or nano eg: export PATH=${PATH}:/Users/Development/HashBaze/flutter/bin
  3. If the above two steps don't work try sourcing both the .zprofile and .zshrc after following the above two steps.

This solution worked for me on macOS Monterey version 12.5