2
votes

npm install -g create-react-app, Why it is mandatory to write before creating any react project. If I globally install npm then why it is required to install npm another time for another folder or project.

It always shows the below error:-

npm ERR! enoent ENOENT: no such file or directory, open 'E:\AvatarDemo\AvatarDemoAwesome\package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent

npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\NAFFY KAUSAR\AppData\Roaming\npm-cache_logs\2020-06-23T15_12_49_522Z-debug.log PS E:\AvatarDemo\AvatarDemoAwesome> npm install -g create-react-app C:\Users\NAFFY KAUSAR\AppData\Roaming\npm\create-react-app -> C:\Users\NAFFY KAUSAR\AppData\Roaming\npm\node_modules\create-react-app\index.js

  • [email protected] updated 1 package in 24.341s npm ERR! code ENOENT npm ERR! syscall open npm ERR! path E:\AvatarDemo\AvatarDemoAwesome\package.json npm ERR! errno -4058 npm ERR! enoent ENOENT: no such file or directory, open 'E:\AvatarDemo\AvatarDemoAwesome\package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\NAFFY KAUSAR\AppData\Roaming\npm-cache_logs\2020-06-23T15_19_27_276Z-debug.log PS E:\AvatarDemo\AvatarDemoAwesome> create-react-app AvatarAwesome
3
You have to use npx create-react-app my-app-name if you got npm install -g create-react-app from a tutorial then find another one.HMR
Ok, but why it is mandatory to write(npm install -g create-react-app) before making any project. Please clear my little bit of confusion.Naffy Kausar
You should not need to write npm install -g create-react-app before every project; the -g flag will install the package globally on your machine, so you can easily run create-react-app in any directory without needing to re-install. The general advice is to uninstall this completely however (npm uninstall -g create-react-app), and instead create new projects by running npx create-react-app app-name so that you are always getting the up-to-date release when setting up a project.Rees Morris

3 Answers

2
votes

Maybe a different way of looking at this the create react package is a website sample site builder. The create react package generates a skeleton app for you with everything you need to build a react app. To build a sample website you need to use the package and pass in a name:

npm install -g create-react-app

The above command installs the builder

 npx create-react-app CustomName

Generates a website from the builder

0
votes

It is not mandatory to use create-react-app when making a React project. It's handy to use it because it initializes the project for you and might save you some work though. For a node project you need a package.json file and this will create one for you if you run it along with some default dependencies needed by React.

Note that the command you talk about (npm install -g create-react-app) just asks npm to install the create-react-app module globally on your computer. It does not run the tool for you. You can run it with the command npx create-react-app my-app which will create a folder called my-app with a package.json and other files in it. It's just a convenience though. You can do all of this file setup yourself if you want to.

The error you're getting is from trying to run npm start (or a similar command) in a folder that does not have a package.json file yet. A package.json file is mandatory for npm and node to know what to do.

So the mandatory action is to create a package.json file. The create-react-app utility is just a handy way to do that if you are making a React app.

0
votes

I was experiencing a similar error and terminal (foolishly, on my part) was is a parent directory instead of the correct one (where package.json was correctly located).

All I did in terminal was cd [insert correct directory name here, overwriting brackets] and that corrected the problem.