Search code examples
node.jsreactjscreate-react-appnpm-installsudo

"sudo npm install -g create-react-app" command not working


I just started learning Reactjs and I am following a tutorial, the first thing we Installed was Nodejs with npm and I installed the latest version without any issue

Node - v14.17.0 npm - 6.14.13

node and npm version

Now the instructor told us the benefits of create-react-app and showed how to install the package on our system using the terminal (he said to use gitbash on windows), he used the command

sudo npm install -g create-react-app

on his Mac and it was successfully installed on his system, he mentioned -g would make sure to install it globally so that we can use it anywhere. He then created a project using

create-react-app hello_world

and showed the folder it created, and also used command

npm start

and showed it works, but when I opened the gitbash on my laptop (OS - Windows 10) and used the command

sudo npm install -g create-react-app

it said sudo: command not found

gitbash error

I tried on cmd and it gave error there as well

'sudo' is not recognized as an internal or external command,
operable program or batch file.

I tried searching on the Internet and YouTube but they are showing different commands to install create-react-app, I want to know why this command is not working on my system?

The npm -v command also runs successfully and gives the version installed so I don't think there is problem with the npm installation and since I am totally new I have no idea what could have went wrong so please help.


Solution

  • "sudo" = "super user do" is the Unix / Linux / Mac Terminal way to run a command with rights to write and change files for the whole system / for all users, stuff that you would do on Windows by "administrator" rights. It is no part of Bash, so Git Bash for Windows does not have or emulate it.

    So just try the command without sudo, i.e.

    npm install -g create-react-app
    

    If you need "sudo" or Windows admin rights (privilege elevation) depends on the configuration of your system as a whole and, in this case, Node.js especially. On my machine currently in front of me (with presets) I need sudo for some "global" Node.js directories, but for others not, on the machine on the desk behind me I don't need sudo at all for Node's "global" dirs. You should always try commands first without "sudo" and wait if the system explicit tells you that you need special rights for that action.

    Note btw that you will find trillions of examples and "solutions" on the internet with "sudo" where no "sudo" is necessary and will rather do damage than help.

    Another story is that your instructor's way is a bit outdated. With current Node.js / NPM version (eg. if you have freshly installed) you would generally not install packages like create-react-app globally, but "locally", i.e. per project, and that quite simple in one line together with the creation of your project with npx instead of npm ("x" vs. "m"):

    npx create-react-app hello_world
    

    Note that the statement "npx always uses the latest version" on https://create-react-app.dev/docs/getting-started/ is a bit misleading: npx does not update existing packages on your system, but it downloads packages which do not exist yet on your machine, and for that will obviously always get the latest version. For that you should not have a package installed globally ("-g") because npx might then use in a few years a globally installed package version you once installed for a short-termed course (and forgot) for a new project instead of the latest version, or, the other way round, your old course project will still work because it uses the package version of its time and not one from five years later with some missing deprecated functions.