📜 ⬆️ ⬇️

A bit about npm-scripts

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

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

pre- / post-myscript


Sometimes, in order to automate some routine, but with a little desire to drag N megabytes of dependencies in the form of different build tools, and in some cases under the impression of an article written by @ substack , developers can start writing in their npm scripts long lines consisting of a list of commands and '&&'. At least one of the exits can be the division of one complex command into several more simple ones. The most remarkable is the fact that when you run your npm run-script myscript script npm run-script myscript , npm will also try to execute commands that are listed in premyscript and postmyscript before and after the execution of your script, respectively. Thus, the 3 complex commands that previously comprised one script can simply be smashed into three scripts.

The code that is responsible for this is pretty simple , and tries to add pre- / post- to the command name and run it if it is not a pre / post command itself and a 'restart' command, which is handled 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 the launch of 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 keys are abbreviations of words, like:
 { 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 do not 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" } 

All you need to do is write "test": "mocha" . A nice bonus of this is that developers for Windows will stop complaining that they do not start anything (the reason is that different separators are used along the way).

Bonus: require on module files


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

Bonus # 2: Easter eggs in npm


There are a couple of funny easter eggs in the npm.

Source: https://habr.com/ru/post/230821/


All Articles