What does the npm build command do?

2

I do not know much about Node, but I understand that npm is a package manager for node.

As far as my view goes with npm I can download the project packages in a more practical way, I can host my project in a simpler way, not needing, for example, to send all the dependencies to GitHub. The issue is that I do not understand the command npm build .

  • What does this command do?
  • What does this command produce as a result of processing?
  • and why is it necessary?
asked by anonymous 03.01.2019 / 18:30

2 answers

2

The file package.json :

All Node projects that use any package manager, such as NPM or Yarn have a package.json file, which defines the overall metrics of the project, identifying the dependencies, package name, and many other information.

  

To learn more about the package.json file and all its features, please refer to package.json a>.

The npm scripts :

One of the features that package managers like NPM or Yarn provide are scripts , which basically consist of " shortcuts" for commands, defined in the file package.json . They also provide hooks for the package life cycle.

An example (file package.json ):

{
  "scripts": {
    "start": "node app.js"
  }
}

Note that above we have defined the script start , which can be invoked by your terminal using npm run start , or, in its reduced form npm start .

The command npm build :

As demonstrated above, the author of a package can define custom scripts for your package. Now imagine that this package needs a build step. This need is very common in projects that use features like TypeScript .

An example:

Because the package can not be published to the NPM record with TypeScript code, it is very common to create a script (usually by convention, with the name build ), to do build of the TypeScript code for JavaScript:

{
  "scripts": {
    "build": "tsc" <-- Comando usado para compilar arquivos TypeScript em JS!
  }
}

This was only one of the examples for npm build , since its use varies according to the project.

Extra reading

03.01.2019 / 22:59
1

npm build

Executes what is specified in the file package.json .

Source.

    
03.01.2019 / 18:48