0

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
├─┬ react@15.4.1 
│ ├─┬ fbjs@0.8.8 
│ │ ├── core-js@1.2.7 
│ │ ├─┬ isomorphic-fetch@2.2.1 
│ │ │ ├─┬ node-fetch@1.6.3 
│ │ │ │ ├─┬ encoding@0.1.12 
│ │ │ │ │ └── iconv-lite@0.4.15 
│ │ │ │ └── is-stream@1.1.0 
│ │ │ └── whatwg-fetch@2.0.1 
│ │ ├─┬ promise@7.1.1 
│ │ │ └── asap@2.0.5 
│ │ ├── setimmediate@1.0.5 
│ │ └── ua-parser-js@0.7.12 
│ ├─┬ loose-envify@1.3.0 
│ │ └── js-tokens@2.0.0 
│ └── object-assign@4.1.0 
└── react-dom@15.4.1 

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.

galfisher
  • 1,122
  • 1
  • 13
  • 24
Anupam
  • 14,950
  • 19
  • 67
  • 94
  • Is your terminal currently set to the root directory of your project, where `package.json` is? What do you get when you run `pwd` (print working directory) and `ls` (list)? Feel free to edit your question with this information. – therobinkim Dec 27 '16 at 07:32
  • I am in the root directory of the project but as I mentioned, I dont have package.json anywhere on my machine – Anupam Dec 27 '16 at 07:39

3 Answers3

5

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!

therobinkim
  • 2,500
  • 14
  • 20
  • Thanks, that did it for me. I wonder why this important piece of information (of running 'npm init') does not appear in react installation documentation (https://facebook.github.io/react/docs/installation.html). – Anupam Dec 27 '16 at 07:40
  • @AnupamJain It's one of those first-step things that doesn't get mentioned bc you're somewhat expected to know it. But now that you know, and you'll never forget! :) (This, too, is how I learned that `npm init` and `package.json` are mandatory.) – therobinkim Dec 27 '16 at 07:41
  • Thanks, I have been trying to mark it as accepted answer but SO wants me to wait for 5 minutes :) Looks like the init bit is more about npm than react which is probably why it does not appear in the react documentation but good to know for newbies (to both React and Node) like me. – Anupam Dec 27 '16 at 07:45
  • **Update**: Now updated the React doc to include `npm init` (https://github.com/facebook/react/pull/8649). Should be visible after the commit gets pushed. – Anupam Dec 29 '16 at 06:54
  • `npm init` does fix the problem and creates `package.json`.However, in my case, react already had a `package.json` (which was not detected by `npm audit fix`). As a result, second `package.json` file will be created. – monkrus Oct 19 '19 at 17:28
2

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 .

Mavrichi
  • 31
  • 2
  • 1
    If you run npm init or npm install again it will just install another package,json there and it will be a mess – Mavrichi Oct 27 '20 at 16:22
0

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.