5

In Create React App we start our app with npm start but for the build, we use npm run build it should be npm run start but how npm start works. is it any default npm script command?

1
  • 1
    npm run start is so common that npm implements a shortcut to save you from typing run. Commented Nov 26, 2019 at 5:25

2 Answers 2

5

There a set of default built in npm scripts that can be executed without the "run"keyword.These are

install, preinstall, preuninstall, postuninstall
prepublish, prepare, prepublishOnly, prepack, postpack, 
publish,preversion, version, postversion, 

pretest, test, posttest: Run by the npm test command.
prestop, stop, poststop: Run by the npm stop command.
prestart, start, poststart: Run by the npm start command.
prerestart, restart, postrestart: Run by the npm restart command. Note: npm restart will run the stop and start scripts if no restart script is provided.

Some even run automatically after a given command (postinstall - after "npm install"). To fully understand these scripts please refer documentation here

In addition to this you can also define custom scripts that can run

  • any command supported by your terminal
  • any command supported by npm.

These user defined custom scripts should be executed using "npm run ... ".

The instructions that need to run on these scripts are defined under the scripts section of the package.json file. In the package.json shown below "start" and "test" are inbuilt, npm recognizable, commands. "build", "myinit", "deletefolder", "hellovnoitkumar" are custom scripts that are custom defined.

The supported npm executions for this package.json are

  • npm start (inbuilt)
  • npm test (inbuilt)
  • npm run build (custom)
  • npm run myinit (custom)
  • npm run deletefolder (custom)
  • npm run hellovnoitkumar (custom)

Sample package.json

//npm start, npm test
//npm run build, npm run myinit, npm run deletefolder, npm run hellovnoitkumar
//*Note that you also can define what each built in npm command does (npm start, npm test).*
{
  "name": "my-webapp",
  "version": "0.1.0",
  "private": true,
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "^2.1.5",
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "myinit" : "npm install && npm run build && npm start",
    "deletefolder": "rm -rf documents",
    "hellovnoitkumar": "echo "hello vnoit kumar""
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

npm has number of built-in commands, which you can run without "run" word, like start, test, publish etc. User defined scripts, on the other hand, need to be used with "run" word. You can also use built-in scripts with "run", it will be pretty equal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.