ChatGPT解决这个技术问题 Extra ChatGPT

What exactly is the 'react-scripts start' command?

I've been working with a React project using create-react-app and I have two options to start the project:

First way:

npm run start with the definition at the package.json like this:

"start": "react-scripts start",

Second way:

npm start

What is the difference between these two commands? And, what is the purpose of the react-scripts start?

I tried to find the definition, but I just found a package with this name. I still don't know what is the use of this command?

"start" is a name of a script, in npm you run scripts like this npm run scriptName, npm start is also a short for npm run start
react-scripts start is the proper command to run the React app in dev mode. This command is stored in package.json so you don't have to memorize it and may simply type the usual npm run start instead. npm start is a shortcut for the latter.

L
Luke

create-react-app and react-scripts

react-scripts is a set of scripts from the create-react-app starter pack. create-react-app helps you kick off projects without configuring, so you do not have to setup your project by yourself.

react-scripts start sets up the development environment and starts a server, as well as hot module reloading. You can read here to see what everything it does for you.

with create-react-app you have following features out of the box.

React, JSX, ES6, and Flow syntax support. Language extras beyond ES6 like the object spread operator. Autoprefixed CSS, so you don’t need -webkit- or other prefixes. A fast interactive unit test runner with built-in support for coverage reporting. A live development server that warns about common mistakes. A build script to bundle JS, CSS, and images for production, with hashes and sourcemaps. An offline-first service worker and a web app manifest, meeting all the Progressive Web App criteria. Hassle-free updates for the above tools with a single dependency.

npm scripts

npm start is a shortcut for npm run start.

npm run is used to run scripts that you define in the scripts object of your package.json

if there is no start key in the scripts object, it will default to node server.js

Sometimes you want to do more than the react scripts gives you, in this case you can do react-scripts eject. This will transform your project from a "managed" state into a not managed state, where you have full control over dependencies, build scripts and other configurations.


Do you know how to run it on production?
to use it in productions you would say npm run build. this will create a build folder. this folder you may then serve. e.g. npm install -g serveand then serve -s build facebook.github.io/create-react-app/docs/deployment
It seems like react-scripts should be included in a project as a devDependency, is that right? Or should it be in the normal dependency section of package.json?
in my opinion, for client side rendered applications, it does not matter if devDependency or not, because the bundler is what matters here. leveraging devDependencies makes sense in node applications
b
bvdb

As Sagiv b.g. pointed out, the npm start command is a shortcut for npm run start. I just wanted to add a real-life example to clarify it a bit more.

The setup below comes from the create-react-app github repo. The package.json defines a bunch of scripts which define the actual flow.

"scripts": {
  "start": "npm-run-all -p watch-css start-js",
  "build": "npm run build-css && react-scripts build",
  "watch-css": "npm run build-css && node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/ --watch --recursive",
  "build-css": "node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/",
  "start-js": "react-scripts start"
},

https://i.stack.imgur.com/AgqTe.png

The blue boxes are references to scripts, all of which you could executed directly with an npm run <script-name> command. But as you can see, actually there are only 2 practical flows:

npm run start

npm run build

The grey boxes are commands which can be executed from the command line.

So, for instance, if you run npm start (or npm run start) that actually translate to the npm-run-all -p watch-css start-js command, which is executed from the commandline.

In my case, I have this special npm-run-all command, which is a popular plugin that searches for scripts that start with "build:", and executes all of those. I actually don't have any that match that pattern. But it can also be used to run multiple commands in parallel, which it does here, using the -p <command1> <command2> switch. So, here it executes 2 scripts, i.e. watch-css and start-js. (Those last mentioned scripts are watchers which monitor file changes, and will only finish when killed.)

The watch-css makes sure that the *.scss files are translated to *.cssfiles, and looks for future updates.

The start-js points to the react-scripts start which hosts the website in a development mode.

In conclusion, the npm start command is configurable. If you want to know what it does, then you have to check the package.json file. (and you may want to make a little diagram when things get complicated).


I just tried creating new project via create-react-app and I have different scripts inside package.json. For example "start": "react-scripts start"
@MáraToner it's perfectly possible that the default setup has evolved to something else by now. It's likely to continue evolving in the future. Fortunately, (as mentioned above) the package.json is configurable to run any CLI tool or script. So, the essence of this answer should still hold valid.
Gorgeous diagram. Is there a tool that can generate that from a package.json file's "scripts" section? That would be absolutely amazing.
j
johndpope

succinctly - it runs this

node node_modules/react-scripts/bin/react-scripts.js start

S
Sagiv b.g

"start" is a name of a script, in npm you run scripts like this npm run scriptName, npm start is also a short for npm run start

As for "react-scripts" this is a script related specifically to create-react-app