⬆️ ⬇️

npm @ 3: the future is here

At the end of June, the first beta of the third version of npm was released. And on September 26, version 3.3.3 was marked as latest in the official repository, which marks the end of beta testing and the availability of the third version for all developers. But the transition to the third version is not very fast. For example, by downloading node.js from the official site, we’ll get the version 2.x npm included. At the moment, developers in parallel support two versions, 2.x and 3.x - almost like in Python. I hope that the transition will be faster with npm, the developers are threatening to coincide with the output of Node 5. In the meantime, they did not do this, I suggest you familiarize yourself with the innovations and find out how you can update the npm to the top three with one team.



Single-level node_modules structure



One of the problems with npm was the tree-like dependency organization inside node_modules . On windows, this generated too long file paths and most programs lost the ability to work with such files. Plus duplication of dependencies: if a popular library was used by several other libraries, then each received its own copy in its personal node_modules . This could be partially fixed with the dedupe utility, but in some cases npm install failed with an error much earlier, simply because the file descriptors were running out on the virtual machine.



In the third version of npm, most dependencies are installed in one, the root node_modules directory. Exceptions will be made to different versions of libraries: if, in the process of studying the dependencies, npm finds that it is necessary to install several different versions of the same library, one of them will be installed in the root directory node_modules , and the rest in local node_modules .



peerDependencies are no longer automatically set.



Among all the ways to specify dependencies, peerDependencies has always stood apart. Using this field, it was possible to specify “inverse dependency” for plug-ins: with which versions of the library or utility the plugin is compatible. If two plugins required an incompatible version of the library, then npm reported an error and stopped the installation. But there was one unpleasant moment: if at the time of installation of the plugin the library was not installed yet, then npm automatically installed it. Moreover, not in the node_modules directory of the plugin, but to a higher level. In general, it is logical, but it led to unpleasant consequences with incorrect versions of the installed dependencies. In the 3rd version, the automatic installation was removed and limited to a warning message. Now, the presence of the library in the list of dependencies should be followed by the one who indicated the plugin in this list.

')

Multi-stage installer



In the previous version, the scripts specified in such scripts fields as preinstall and postinstall were executed during the installation of the dependency. At the same time there was no guarantee that when executing 'postinstall' all child dependencies will be installed. Which led to a lot of problems . In the third version, the installation is divided into steps:



  1. npm downloads all dependencies
  2. running the 'preinstall' script for all dependencies
  3. npm installs the downloaded dependencies in the node_modules directory (and subdirectories in case of version conflicts).
  4. the 'postinstall' script is executed for all installed dependencies.


This change made it possible to get rid of a large number of “long-running” problems and made the execution of scripts during installation simple and straightforward. I hope.



Little things in stock



In addition to the three major changes, the third version of npm has many minor ones. This is a new display of the progress of the installation with the progress bar, and the command line keys --only = / --also = , which made --dev deprecated and a lot of small things. I highly recommend reading the change history in the releases section. There's a totally freaky girl community manager who describes the change not just with a twinkle - she is burning napalm over the squares. Look, you will like it.



To install the latest version of npm, just run the following command line spell:



npm install -g npm@3 


Under OSX, there may be problems if npm was installed using homebrew. For this configuration, the spell is slightly different:



 cd /usr/local/lib/node_modules/npm npm install -g npm@3 




Update from 11/01/2015





As promised, the new release of node.js version 5.0 uses the third version of npm. Now for most developers: goo.gl/04k5ny

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



All Articles