npm cant find package.json when installing react

16.6k views Asked by At

This is a followup question to my earlier question (but is an independent question)

I am trying to install react and react-dom on a Mac:

npm install --save react react-dom

but get the following warnings (path names replaced with ...):

npm WARN saveError ENOENT: no such file or directory, open '/Users/../Z/package.json'
/Users/.../Z
├─┬ [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├─┬ [email protected] 
│ │ │ ├─┬ [email protected] 
│ │ │ │ ├─┬ [email protected] 
│ │ │ │ │ └── [email protected] 
│ │ │ │ └── [email protected] 
│ │ │ └── [email protected] 
│ │ ├─┬ [email protected] 
│ │ │ └── [email protected] 
│ │ ├── [email protected] 
│ │ └── [email protected] 
│ ├─┬ [email protected] 
│ │ └── [email protected] 
│ └── [email protected] 
└── [email protected] 

npm WARN enoent ENOENT: no such file or directory, open '/Users/.../Z/package.json'
npm WARN Z No description
npm WARN Z No repository field.
npm WARN Z No README data
npm WARN Z No license field.

I searched around a bit but did not find a good reason for these warnings. SO posts like these seem to suggest installing react in the same directory where node modules are installed. My node is installed in usr/local/bin but package.json does not appear anywhere on the machine even on a global search. I just installed npm before trying to install react so don't think its a versioning issue.

3

There are 3 answers

5
therobinkim On BEST ANSWER

If this is a brand new project, it can be resolved by running npm init from the root directory of your project (where you want package.json to be created), and press "ENTER" at all of the prompts to accept the default answers. (It is easy to change your responses by directly modifying the package.json file later.)

Once the package.json has been created, you will be able to run npm install commands!

0
Naveen Kummari On

Check the folder you're in. Usually after using npx create-react-app, you'll be in the same folder. you will be needed to cd into the new folder that is created after running above command. there you'll find package.json.

1
Mavrichi On

Actually for me it just worked like this : I ran npx create-react-app "nameOfProject" , if you type npm start in the same folder it won't work because it tries to find the package.json in the same folder , when your package.json is in the subfolder ( the folder of the "nameOfProject" ). Run cd "nameOfProject" to change the directory and them run npm start .