Should npm run start be used instead of npm start in Create React App?

The starting point for our application in Create React App is npm start. However, when we want to build the app, we use npm run build instead of npm run start. It may be confusing why npm start works this way. Is it a default npm script command?

Answer №1

There are default built-in npm scripts that can be executed without using the "run" keyword. These include:

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

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

Some of these scripts automatically run after a specific command (like postinstall - after "npm install"). For a complete understanding of these scripts, refer to the documentation here.

In addition, you can define your own custom scripts that can run:

  • Any terminal-supported commands.
  • Any npm-supported commands.

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

The instructions for these scripts are defined under the scripts section of the package.json file. In the example package.json below, "start" and "test" are built-in npm commands, while "build", "myinit", "deletefolder", "hellovnoitkumar" are custom scripts.

The supported npm executions in this package.json are:

  • npm start (built-in)
  • npm test (built-in)
  • 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
//*You can also 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""
  }
}

Answer №2

When working with npm, there are a variety of built-in commands available that can be executed without the need for the "run" keyword. These include commands such as start, test, and publish. However, user-defined scripts must always be preceded by the "run" keyword. It is also possible to use built-in scripts with the "run" keyword for consistency.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues have arisen with the execution of JavaScript code inside a callback function

When the warnBeforeNew variable is set to false in the JavaScript code below, the file open function works as expected. However, when warnBeforeNew is true, an error occurs stating "Uncaught TypeError: Cannot read property 'root' of undefined". ...

Executing npm webpack within mvn release:perform task - a step-by-step guide

My goal is to generate RC versions for the application using Maven release:prepare and release:perform goals on Jenkins server. The sequence of events includes npm install, npm run build (to execute webpack), mvn release:prepare (which generates correct RC ...

An effective way to mimic an un-exported (private) function within a user module using Jest

In my code, I have a function that handles API requests called "private" and several other functions that initiate specific requests with configuration objects. For example, in the requestUploadStatementFile file. I want to test these public functions, bu ...

Incorporate a captivating background image into your SVG design

Currently working with Snap.svg, my aim is to include a background image on a polygon shape. The specific polygon in question is outlined as follows: <svg id="test" height="600" width="600" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= ...

Issues with HTML5 video playback in Apple machines using the Firefox browser

On a website I'm developing, I've included an HTML5 video that works perfectly except for one specific issue - it won't play on Firefox in Apple devices. Surprisingly, it functions well on all other browsers and even on Firefox in Windows an ...

Executing a setInterval function in NodeJs without prolonging the process's lifespan

To better grasp what I need to do, I simplified the task at hand: we have a lengthy nodejs process with numerous queued async functions, making it impossible to predict when they will finish. Our goal is to create an update process that stores the current ...

How to apply a class on button click using react.js

After spending a few weeks diving into React, I've got the hang of the basic syntax like props and states. However, I'm hitting a roadblock when it comes to connecting certain concepts, especially adding classes when a state changes. My current p ...

Encountered an undefined error while trying to read promises

I'm attempting to receive a response from a function in order to trigger another function, but I am not receiving the expected response. I encountered the following error message: "TypeError: Cannot read property 'then' of undefined." In my ...

Is this code in line with commonly accepted norms and standards in Javascript and HTML?

Check out this Javascript Quiz script I created: /* Jane Doe. 2022. */ var Questions = [ { Question: "What is 5+2?", Values: ["7", "9", "10", "6"], Answer: 1 }, { Question: "What is the square root of 16?", Values: ["7", "5", "4", "1"], Answer: ...

Tips for rendering the stencil as invisible and disabled while still allowing other features (such as rotating and flipping) to remain enabled within the react-advanced-cropper

I am facing a challenge where I need to hide and disable the stencil and overlay in react-advanced-cropper, while keeping all other features like rotation and flipping enabled. Is there a specific property in this library that allows me to disable the st ...

Commitments and incorporating items from an array into objects nested within a separate array

My current project involves a command line node application that scrapes valuable data from a specific website and stores it in a CSV file. For the scraping functionality, I am utilizing scrape-it, which enables me to successfully extract all the necessa ...

Proper functionality of Chrome Extension chrome.downloads.download

I am currently developing an extension with a feature that allows users to download a file. This file is generated using data retrieved from the localStorage in Chrome. Within my panel.html file, the code looks like this: <!DOCTYPE html> <html&g ...

The pagination feature of the material-ui data grid is experiencing issues with double clicks because of its compatibility with the react-grid-layout library for

I am currently using the react-grid-layout library to manage the resizing of both charts and a material-ui data grid table. However, I am encountering an issue where when clicking on the table pagination arrow, it does not work properly. I have to click tw ...

Searching for data in Express JS with MongoDB using the $or operator is not possible

I am currently developing an API for managing my invoice data. Within this data, there are boolean values that determine the status of the invoices. For example, some invoices are marked as 'payment received', and I need to search for those speci ...

Enhancing functionality by updating a function to accept an object as input instead of individual key:value pairs in TypeScript

I'm currently tackling a challenge with a friend's icon library component, specifically with their set() function. The issue arises when I want to invoke two functions, namely setRandomColor() and setColor(), both intended to update two values w ...

How to extract a JavaScript object from an array using a specific field

When dealing with an array of objects, my goal is to choose the object that has the highest value in one of its fields. I understand how to select the value itself: Math.max.apply(Math, list.map(function (o) { return o.DisplayAQI; })) ... but I am unsur ...

Is it possible to enable tab navigation for a button located within a div when the div is in focus?

I have a component set up like this: (Check out the Code Sandbox example here: https://codesandbox.io/s/boring-platform-1ry6b2?file=/src/App.js) https://i.sstatic.net/ZuxdL.png The section highlighted in green is a div. Here is the code snippet: import { ...

What is the best way to insert a JavaScript variable into a filter while using ng-repeat?

I'm currently working on a situation where I have the following code: <div ng-repeat="i in MediaFeedItems | filter: { category: 'userSelect' }" class="mediafeed--item"> This specific code snippet is responsible for iterating through ...

switching the color of a path in an SVG when hovering over a

In the midst of working on SVG, I'm trying to implement a color change effect upon hover. The desired functionality is such that when hovering over the .business div, the color of the SVG business path should also change accordingly as specified in th ...

Discovering the total of varying inputs in ReactJS

//this is the API response: { "message": "success", "code": 100, "data": { "inCourseCategories": [ { "_id": "62b842f09184bf2330e6f506", "course": "601a67e6db65fb15946e6b6f ...