npm start script returns error immediately after i

2020-02-15 03:57发布

问题:

As the title states, I just installed the react library and went to start the application for the first time. I'm getting the following error below.

From what I can tell, it seems that the application is looking into the wrong folder to find the starter script. I've not altered any of the initial starter files, or added any of my own. I'm unsure how to address this issue.

npm ERR! errno -4058
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\David\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\David\AppData\Roaming\npm-cache\_logs\2019-12-28T21_32_24_438Z-debug.log```

回答1:

I think you forgot to cd into (or to change to) your project's directory.

Assuming that you initialised your project in the directory C:\Users\David and gave the project name my-app, just type cd my-app (or to be precise type cd C:\Users\David\my-app\ in your Command Prompt and then run npm start.

The file the error message is complaining about, package.json, is already available inside your project folder.

However, installing create-react-app and then initialising a new app is not the new recommended way.

According to the React documentation, uninstall the create-react-app global npm package and follow the following steps to create a new React app:

npx create-react-app my-app
cd my-app
npm start

Note

npx on the first line is not a typo — it’s a package runner tool that comes with npm 5.2+.