A bit about npm-scripts

If someone is not familiar with the subject area, then NPM , the node package manager (analogue of bundler / composer / easy_install / nuget), is a tool for managing dependencies in your Node.js project, which, in combination, can perform a number of useful functions. Written by Isaac Schlüter , who promoted quite radically at the beginning of his story (sending out pull requests to all existing Node.js projects with the addition of package.json - a dependency manifest).

All of the things listed below are known to many, but for some it can be a pleasant discovery, which will save a certain amount of typed characters.

pre- / post- myscript


Sometimes, in order to automate some routine, but with little desire to drag along N megabytes of dependencies in the form of different build tools, and in some cases, inspired by the article by @ substack , authors can start writing in their npm scripts long lines consisting of a list of commands and '&&'. At least one of the ways out is breaking up one complex team into several simpler ones. The most remarkable thing is that when you run your script npm run-script myscript, npm will also try to execute the commands that are entered in premyscript and postmyscript before and after executing your script, respectively. Thus, 3 complex teams that used to make up one script,

The code that is responsible for this is pretty simple , and tries to add pre- / post- to the name of the command and run them if it is not a pre / post command and a restart command, which is processed in a special way.

npm run instead of run-script, npm i instead of npm install


The title, in principle, describes everything: very often instead of “npm run-script” you can see “npm run”, or “npm i”, which corresponds to running the “npm install” command. The code that does this is simple : using the abbrev-js library, all the commands from the list turn into a huge dictionary, in which the abbreviations are words, of the form:
{
i: 'install',
in: 'install',
ins: 'install',
...

And runs the appropriate command.

No need to write paths for executable files in scripts


Sometimes npm scripts are used to run locally installed npm packages with binaries. But often people don’t know about one nice feature: npm at runtime adds the node_modules / .bin folder to the PATH variable. That is, if you have a locally installed mocha, you do not need to write
"scripts": {
  "test": "./node_modules/.bin/mocha"
}

And just write "test": "mocha". A nice bonus to this is that the developers for Windows will stop complaining that they do not start anything (the reason is that different delimiters are used on the way).

Bonus: require on module files


The require command, it turns out, when working with module names (and not relative paths), it can not only load the index.js / index.node file from the root of our module, which is installed in node_modules, but also provide the ability to load an arbitrary file from inside the module (violating any encapsulation, but can sometimes be useful). Therefore, you can do it require('module/lib/hidden-stuff').

Bonus # 2: easter eggs in npm


In NPM there are a couple of funny easter eggs .

Also popular now: