What does the npm build command do?

Asked

Viewed 9,712 times

3

I don’t know much about Node, but I understand that the npm is a package manager for Node.

As far as my vision goes 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 point is I don’t understand command npm build.

  • what this command does?
  • what this command produces as a result of processing?
  • and why it is necessary?

2 answers

3


The archive package.json:

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

To learn more about the archive package.json and all its functionalities, please refer to documentation of package.json.

The npm scripts:

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

An example (file package.json):

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

Note that above we define the script start, that 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 scripts customized for your package. Imagine now that this package needs a build. This need is very common in projects that use resources such as Typescript.

An example:

Since the package cannot 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 the build Typescript for Javascript code:

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

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

Extra reading

  • Excellent reply, thank you !

1

npm build

Executes what is specified in the file package.json.

Source.

  • When running it he used three js files and generated only one js Minify, as this fact fits in the explanation given ? Thank you for the answer

  • What type of project are you running on? And how is your package.json file?

  • You are running on what type of project? I’m not sure how to answer this question, but I’m executing the command npm build under this project

  • in this case it will execute the command "webpack -p", which is specified in package.json.

  • https://github.com/h5p/h5p-drag-question/blob/master/package.json

  • I did the translation wrong, I’ll edit the answer

  • It was simpler than I thought, I did the editing by correcting my mistake.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.