# npm memo
# links
- forum officiel lancé le 30 mai 2018
- npm basic commands
- 10 Cool Things You Probably Didn’t Realize npm Could Do
- npm-stat : generate download charts for any package on npm
- Why I Left Gulp and Grunt for npm Scripts - Cory House - 20160117
- One-line node modules (with a big answer by sindresorhus)
- Understanding the npm dependency model - 20160824
- How to use NPM to package a deployment? - stackoverflow.com - 20150221 (For your question "Shouldn't I output the production files as part of the build process, and publish these as a versioned artifact with NPM": you could, but this is not what most people do. As long as your build process is repeatable, there is no reason to package and publish the built version of your app. I am assuming here that your are building an application and not a reusable library in which case it would be a different story.)
- How well do you know your npm trivia? - 20180528
What’s npm’s mascot? a wombat
What does “npm” stand for? Nothing! The original ‘npm’ software was used for Node.js package management
How many people does npm employ? npm, Inc. employs 34 people, distributed worldwide
What is the largest number of packages that have been downloaded from npm’s Registry in one week? From May 10th to May 17th of 2018, npm users downloaded 5.2 billion packages from the npm Registry, setting a new record.
How big is the npm Registry? The registry is approximately 8TB in size, with 25M total package-versions.
# npm 5
Features list
- better perfs
- save behavior in
package.jsonwhen installing a package is reversed (saved by default, flag needed to not save) - lockfile strategy changed with
package-lock.jsongenerated by default (behavior regarding to the registry is different then shrinkwrap) - the deps tree is not displayed after a npm install, print a summary instead
- complete rewrite of the cache system.
npm cache cleanis must be run before using npm 5. new commandnpm cache verify, new options to tweak cache behavior (--prefer-offline,--prefer-onlineand--offline), concurrent access support.
v5.0.0 - blog.npmjs.org - 20170526
Here’s what you need to know about npm 5 - blog.pusher.com - 20170613
Tout savoir sur la nouvelle version de npm : npm 5 - maxlab.fr - 20170528
npm@5 — Yarn killer? - medium.com - 20170615
# npm 5.1
npm lsshows all deps even missing onesnpm-package.locksyncs when runningnpm install <package-name>
# npm 5.2
- npx added, used to replace
npm run-scriptcommand. Associated with the newnodepackage, you can run a package against a specific node version.
# npm 5.3
lots of npx fixes
# npm 5.4
- new "did you mean" feature on CLI when making spelling mistakes on the commands
npxfixes for Windows platform
# npm 5.5
- new security features for the registry (2 factors auth, tokens, ...)
# npm 5.6
- ability to generate
package-lock.jsonwithout runningnpm install - fix npm and node9
# npm 5.7
npm installwill automatically resolvegit mergeconflicts inpackage-lock.jsonandnpm-shrinwrap.jsonfiles. cf doc- new
npm cicommand. Install from lock file only and so report errors when a dep is not fixed.node_modulesare always rewritten to achieve that.
# npm 5.8
- detect-newline (no more
LFby default) NO_COLORstandard support through var envNO_PROXYsupport through CLI with--no-proxy- better env var support for
.npmrc(cf PR) - better error msg for
JSON parse errorandEPERM/EACCES npm cifix and perf work
# npm 6
- npm 6.0.0, le gestionnaire de paquets officiel de Node.js. passe en @latest, et se concentre désormais sur la sécurité
- npm inc s'offre Lift Security et sa plateforme NSP
- support natif des hooks npm (port de wombat)
- intégration des packages
create-xdans la commandenpm init. Lescreate-xsont des packages dédiés à la génération d'applications. Par exemple le très connucreate-react-apppermet de générer un projet de démarrage pour react. En intégrant l'exécution de ce type de package ànpm init, cela permet au développeur de partir de la création d'un nouveau projet via npm, donc de configurer son proprepackage.jsontout en déclarant quel packagecreate-xil souhaite utiliser, lequel sera exécuté dans la foulée. Par exemplenpm init react-appabouti à la génération de votrepackage.jsonpuis à l'exécution decreate-react-app - ajout de la commande
npm auditqui comme expliqué dans le paragraphe précédent fournit un audit de sécurité des dépendances de votre projet. Cette fonctionnalité sera pleinement fonctionnelle d'ici quelques semaines, l'application gérant le registre n'ayant pas encore été mise à jour pour supporter cette fonctionnalité. L'exécution de cette commande aboutit pour l'heure à ce message d'erreur :Your configured registry (https://registry.npmjs.org/) does not support audit requests. - les versions taguées
deprecateddes packages ne sont plus installées lorsque c'est possible - les commandes
npm updateetnpm outdatedtiennent désormais compte du taglatest - abandon du support de Node.js 4 et 7
- diverses corrections de bogues
- des mises à jour de dépendances
# npm 6.0.1
- npm : la version 6.0.1 du gestionnaire de paquets officiel de Node.js. passe en @latest, la commande npm audit est désormais fonctionnelle
- un meilleur support des vieux fichiers
npm-shrinkwrap.jsonpubliés sur le registre - une correction sur la gestion de l'interruption de l'installation via
CTRL+C - des améliorations de la commande
npm audit
- un meilleur support des vieux fichiers
# npm 6.2.0
- npm : la version 6.2.0 du gestionnaire de paquets officiel de Node.js passe en @latest, avec quatre nouveautés d'importance autour
- La principale nouvelle fonctionnalité est la commande
npm audit fix - Ajout d'une option
--parseablesur la commandenpm auditpour permettre de chainer facilement des commandesgrepouawk - Ajout du support de la signature des commits Git lors de l'exécution de
npm versionvia l'option de configuration npmsign-git-commit - npm rejoins ECMA International et donc le TC39
- La principale nouvelle fonctionnalité est la commande
# npm 6.3.0
- v6.3.0
npm versionnow supports a--preidoption to specify the preid for prereleases. For example,npm version premajor --preid rcwill tag a version like2.0.0-rc.0
# npm 6.4.0
- v6.4.0
- configurable audit level for non-zero exit
npm auditcurrently exits with exit code 1 if any vulnerabilities are found of any level. Add a flag of--audit-leveltonpm auditto allow it to pass if only vulnerabilities below a certain level are found. Example:npm audit --audit-level=highwill exit with 0 if only low or moderate level vulns are detected npm auditnow shows no vulnerabilities for npm itself!
- configurable audit level for non-zero exit
# npm 6.5.0
- v6.5.0
- Backronym
npm citonpm clean-install
- Backronym
# npm 6.9.0
- v6.9.0
- Add support for package aliases. see RFC 0001-package-aliases.md
- Make empty-string run-scripts run successfully as a no-op
# npm 6.10.0
- v6.10.0
- enable production flag for
npm audit: enables using the existing--productionflag when runningnpm audit. Using this flag will ignore dev dependencies when running an audit (see PR)
- enable production flag for
# npm 6.11.0
# npm 6.13.0
# npm 6.14.0
# npm 7
- Release v7.0.0 - 20201013
- npm v7 Series - Introduction
- Reduce noise that is not actionable
- Manage your packages for you
- Strict separation of concerns (refacto)
- Be as fast as possible while behaving correctly (perfs improv)
- The JavaScript coders guide to getting more from GitHub and NPM - GitHub Satellite 2020
- npm v7 Series - Arborist Deep Dive
@npmcli/arboristis the dependency tree manager for npm, new in npm v7. It provides facilities for doing nearly everything that npm does with package trees, and fully replaces large parts of the npm CLI codebase.- Many of the features and changes in npm v7 are related to the refactor to use Arborist for all of npm’s tree management work.
- better performance, more predictability, faster feature delivery, and fewer bugs.
- the core problem, which has led to a lot of excess work and bugfixing in the npm CLI codebase, is that
read-package-treedid not properly differentiate between symlinked dependencies and regular installed dependencies, when creating the logical tree of nodes. - this new dependencies tree manager opens several new features for npm v7 :
- a new
--prefer-dedupeoption is added to tell the tree building algorithm to prefer deduplication over getting the latest version of a dependency. - automatic install of
peerDependencies(was dropped in npm v4 because of several not resolvable issues). - better staging folders management for rollback when npm install fails on Windows platform because of Windows folder locking behaviors.
- implementation of workspaces.
- a new
- npm v7 Series - Why Keep
package-lock.json?- if a nutshell,
yarn.lockis not fitted for npm.
- if a nutshell,
- npm v7 Series - Beta Release! And: SemVer-Major Changes in npm v7
- list all the breaking changes introduced.
- the bigger is the installation of
peerDependencieswhen running CLI installation commands (npm install,npm ci, etc ...). - workspace support added.
# workspaces
Attempt by npm team to have multi-package monorepo built-in support like what is provided by lerna.
Add a set of features to the npm cli that provide support to managing multiple packages from within a singular top-level, root package.
See Workspaces in Yarn - classic.yarnpkg.com/blog
# semver
semver to manage scopes in package.json
- semver :
<major>.<minor>.<patch> - caret range keep major, change minor & patch
- tilde range keep major & minor, only patch update
Warning semver interpretation for breaking changes can be slightly different from a package to another. The semver spec says that any breaking change must be traduced in a ++ on the major version number. But libs like angularjs (or protractor) introduce breaking changes in minor update. Consequences : ^ (caret) in package.json should allow automatic feature update without breaking changes but in fact it depends a lot on the package owner management.
# npm scripts
npm-scripts How npm handles the "scripts" field - docs.npmjs.com
npm supports the "scripts" property of the package.json file, for the following scripts:
prepublish: Run BEFORE the package is packed and published, as well as on localnpm installwithout any arguments. (See below)prepare: Run both BEFORE the package is packed and published, on localnpm installwithout any arguments, and when installing Git dependencies (See below). This is run AFTERprepublish, but BEFOREprepublishOnly.prepublishOnly: Run BEFORE the package is prepared and packed, ONLY onnpm publish. (See below.)prepack: run BEFORE a tarball is packed (onnpm pack,npm publish, and when installing Git dependencies)postpack: Run AFTER the tarball has been generated and moved to its final destination.publish,postpublish: Run AFTER the package is published.preinstall: Run BEFORE the package is installedinstall,postinstall: Run AFTER the package is installed.preuninstall,uninstall: Run BEFORE the package is uninstalled.postuninstall: Run AFTER the package is uninstalled.preversion: Run BEFORE bumping the package version.version: Run AFTER bumping the package version, but BEFORE commit.postversion: Run AFTER bumping the package version, and AFTER commit.pretest,test,posttest: Run by thenpm testcommand.prestop,stop,poststop: Run by thenpm stopcommand.prestart,start,poststart: Run by thenpm startcommand.prerestart,restart,postrestart: Run by thenpm restartcommand. Note: npm restart will run the stop and start scripts if norestartscript is provided.preshrinkwrap,shrinkwrap,postshrinkwrap: Run by thenpm shrinkwrapcommand.
Additionally, arbitrary scripts can be executed by running npm run-script <stage>. Pre and post commands with matching names will be run for those as well (e.g. premyscript, myscript, postmyscript). Scripts from dependencies can be run with npm explore <pkg> -- npm run <stage>.
# command list
# list globally installed package by name
npm -list -g <package_name>
# or for all packages
npm list -g -depth=0
# to get tree deps
npm list -g -depth=1
2
3
4
5
6
7
8
# list locally installed package by name
npm -list <package_name>
2
# view version on repository
npm view <package_name> version
2
# display npm config
npm config list -ls
2
11 Simple npm Tricks That Will Knock Your Wombat Socks Off - 20160826
Introducing hooks: get notifications of npm registry and package changes as they happen - 20160601
# npm init + create-* packages
npm initcan now be used to invoke custom scaffolding tools. You can now do things likenpm init react-appornpm init esmto scaffold an npm package by runningcreate-react-appandcreate-esm, respectively.
See 'feat: add npm init <create-pkg-name> command' PR by John Dalton.
See also npm init official documentation.
# npx
Executes
<command>either from a localnode_modules/.bin, or from a central cache, installing any packages needed in order for<command>to run.By default, npx will check whether
<command>exists in $PATH, or in the local project binaries, and execute that. If<command>is not found, it will be installed prior to execution.Unless a
--packageoption is specified,npxwill try to guess the name of the binary to invoke depending on the specifier provided. All package specifiers understood bynpmmay be used withnpx, including Git specifiers, remote tarballs, local directories, or scoped packages.If a full specifier is included, or if
--packageis used, npx will always use a freshly-installed, temporary version of the package. This can also be forced with the--ignore-existingflag.
npx added, used to replace npm run-script command.
yarn equivalent of npx ? - yarnpkg/yarn on GitHub - 20170715
zkat (npx maintainer) comment
I'm working on library-ifying npx. It's not a huge task to grab the existing npx code and just replace the npm-related guts with the yarn-equivalent commands.
I won't add that directly to npx itself, since it's meant to be agnostic: npx performs no operations which clash with people using other package managers. It doesn't even require npm to be on the system, so you can
npm rm -g npmand npx will work just fine. So you could say npx isypx, unless you feel really strongly about cache-sharing, which is a pretty thing.(in re inspiration: npx is primarily inspired by this long-standing feature request: npm/npm#6053. Most of its functionality centers around fulfilling this need. The auto-install feature was added post-yarn-create, and is definitely intended to be an actual generalized solution to that particular thing -- but it does way more than that)
# testing packages locally
Node.js — How to test your new NPM module without publishing it every 5 minutes - updated 201804
Testing NPM alpha / beta / rc packages - 20180508
3 options to require a your package in your hosting project :
npm link
It will symlink your project in the global node_modules directory.
So it pollutes the global npm namespace.
No preinstall/postinstall hooks will be triggered so if you need to tests them it's not a good choice.
npm link <dep-name> will not alter the package.json file of the hosting project.
In your dependency directory :
npm link # create a global symlink to the local "dependency-name" project
In your hosting project :
npm link dependency-name # create a symlink locally to global dependency-name
npm install /absolute/local/path
It works as if your package was is the npm registry. But it will write the absolute directory path to reach the dependency
in the package.json of your hosting project :
npm install /absolute/path/to/dependency-name
yields this in our package.json :
"dependencies": {
"dependency-name": "file:../../projects/dependency-name",
},
2
3
"dependencies": {
"viking": "file:../../oresoftware/viking",
},
2
3
npm pack
The npm pack command create the tarball that will be pushed in the registry.
So we can build the payload and test against it before pushing it to the registry.
Theses kind of tests are named smoke-tests (cf discussion on stackoverflow).
Don't forget to use a .npmignore file (cf npm doc) to avoid putting editor config files (.idea/ for ex) in the tarball.
Notice that npm publish run npm pack so ignore .tgz files to avoid adding it to the uploaded package.
In your dependency directory :
npm pack # create the tarball in the root dep project directory
In your hosting project you can now install it like a regular package, there is no difference instead that :
- it will not be downloaded from the network but from your local filesystem
- there will be also an absolute path in your host project
package.jsonfile
# npmc testing @next npm version
npm versions tagued @next can be tested without upgrading your current npm version with the npmc package.
c is for canary.
# npm folders
TLDR :
- Local install (default): puts stuff in
./node_modulesof the current package root. - Global install (with
-g): puts stuff in/usr/localor wherever node is installed. - Install it locally if you're going to
require()it. - Install it globally if you're going to run it on the command line.
- If you need both, then install it in both places, or use
npm link.
# managing dependencies
# built-in commands
# list all the installed dependencies in stdout in a tree format
npm ls
# list all the installed dependencies in stdout in a tree format limited to the first level
# it must matches deps listed in package.json
npm ls --depth=0
# same as above but with the deps of deps in the tree
npm ls --depth=1
# print extended informations
npm ll
npm la
2
3
4
5
6
7
8
9
10
11
12
13
# check the registry to see if any (or, specific) installed packages are currently outdated
npm outdated
2
# fixing deps
Since npm v5, the lockfile standard is no more shrinkwrap, it is now the package-lock.json file.
The big difference is that this file is generated automatically by npm when running the install command.
There is also big behavior differences regarding to the registry publication for packages.
npm-shrinkwrap.json is backwards-compatible with npm versions 2, 3, and 4, whereas package-lock.json is only recognized by npm 5+
# understanding peer dependencies
docs.npmjs.com - package.json - peerdependencies
you want to express the compatibility of your package with a host tool or library, while not necessarily doing a
requireof this host
{
"name": "tea-latte",
"version": "1.3.5",
"peerDependencies": {
"tea": "2.x"
}
}
2
3
4
5
6
7
This ensures your package tea-latte can be installed along with the second major version of the host package tea only.
This leads to
├── tea-latte@1.3.5
└── tea@2.2.0
2
instead of
├── tea-latte@1.3.5
└── tea@2.2.0
2
since npm@3, npm is not installing automatically peerDeps. You need to add it explicitly in your package.json.
# peerDeps usage
Mostly for a package to be used as a deps for another. No usage for an end project (like a frontend or a backend).
# peerDeps tools
nathanhleung/install-peerdeps - github.com
A command-line interface to install an NPM package and its peer dependencies automatically.
# others packages
Check for outdated, incorrect, and unused dependencies.
Find newer versions of dependencies than what your package.json or bower.json allows
Examine a package's dependency graph before you install it