# node.js



[Node.js](https://nodejs.org/fr/)® est un environnement d’exécution JavaScript construit sur le [moteur JavaScript V8 de Chrome](https://developers.google.com/v8/).

[npm](https://www.npmjs.com) est un gestionnaire de paquets pour JavaScript automatiquement installé avec Node.

[nvm](https://github.com/creationix/nvm) (Node Version Manager) est un gestionnaire de version de Node.



### Installation (Homebrew):

```bash
# installer node et npm

$ brew install node
```



### Installation (directe):

Installer [Node.js](https://nodejs.org/en/download/) (version LTS)



### Installation ([nvm](nvm.md)):

```bash
$ brew install nvm

# installer Node 8
$ nvm install 8

# installer Node 10
$ nvm install 10
```



### npm

npm est un gestionnaire de paquets pour JavaScript automatiquement installé avec Node.

#### Version de npm:

```bash
# version:
$ node -v
v9.5.0
$ npm -v
5.6.0
```

 

#### Update:


```bash
$ npm install npm@latest -g

$ brew install node

$ npm install npm --global
```

https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-2-change-npms-default-directory-to-another-directory



#### Local:

Installer npm packages localement => `/Users/bruno/Sites/node_modules`

- Installer: `npm install <package_name>`
- Mise-à-jour: `npm update` (depuis le répertoire du module)
- Desinstaller: `npm uninstall <package_name>`

<u>Mises à jour disponibles:</u>

```bash
bruno@silverbook:~/Sites/node_modules$ npm outdated
Package        Current  Wanted  Latest  Location
jquery         MISSING   3.2.1   3.2.1
livephotoskit   1.4.11   1.5.2   1.5.2
```

`npm outdated` montre plusieurs choses: 

1. Current: la version actuelle du module.
2. Wanted: la dernière version qui satisfait le semver ranges decrit dans le package.json 
3. Latest: la toute dernière version du module disponible sur npm.

<u>Mettre-à-jour:</u>

```bash
$ npm outdated | awk '{print $1}' | xargs npm update
```



#### Global:

Installer npm packages globalement => `/usr/local/lib/node_modules/`

- Installer: `npm install -g <package_name>`
- Mise-à-jour: `npm update -g <package_name>`
- Désinstaller: `npm uninstall -g <package_name>`



#### npm verb outdated not updating @angular/cli because it's currently at the maximum version that matches its specified semver range:

[Stackoverflow](https://stackoverflow.com/questions/39758042/npm-update-does-not-do-anything) [semver range](https://docs.npmjs.com/misc/semver#advanced-range-syntax)

```bash
$ npm update -g --verbose @angular/cli

npm verb outdated not updating rxjs because it's currently at the maximum version that matches its specified semver range
```



#### Plus info sur une commande:

```bash
# Mode verbose:
$ npm update -g --verbose

# Exécute la commande sans rien installer:
$ npm install -g --dry-run
```




#### Aller dans node_modules:

```bash
bruno@SilverBook:~$ cd Sites/node_modules/
```

puis

- Liste package installé: `npm ls` `npm list -g --depth=0`
- Mises à jour disponibles: `npm outdated`
- Installer une m-à-j: `npm update`
- Installer un package: `npm install package`
- Désinstaller un package: `npm uninstall package`



#### Voir toutes les versions disponibles d'un module:

```bash
# Option: --json

$ npm show @popperjs/core versions
[
  '2.0.0-alpha.1', '2.0.0-alpha.2', '2.0.0-alpha.3',
  '2.0.0-alpha.4', '2.0.0-next.5',  '2.0.0-next.6',
  '2.0.0-next.7',  '2.0.0-next.8',  '2.0.0-next.9',
  '2.0.0-next.10', '2.0.0-next.11', '2.0.0-next.12',
  '2.0.0-next.13', '2.0.0-next.14', '2.0.0-next.15',
  '2.0.0-next.16', '2.0.0-next.17', '2.0.0-rc.1',
  '2.0.0-rc.2',    '2.0.0-rc.3',    '2.0.0',
  '2.0.1',         '2.0.2',         '2.0.3',
  '2.0.4',         '2.0.5',         '2.0.6',
  '2.1.0',         '2.1.1',         '2.2.0-bundlephobia.1',
  '2.2.0',         '2.2.1',         '2.2.2',
  '2.2.3',         '2.3.0',         '2.3.1',
  '2.3.2',         '2.3.3',         '2.4.0',
  '2.4.1',         '2.4.2',         '2.4.3',
  '2.4.4',         '2.5.0',         '2.5.1',
  '2.5.2',         '2.5.3',         '2.5.4',
  '2.6.0',         '2.7.0',         '2.7.1',
  '2.7.2',         '2.8.0',         '2.8.1',
  '2.8.2',         '2.8.3',         '2.8.4',
  '2.8.5',         '2.8.6',         '2.9.0',
  '2.9.1',         '2.9.2'
]
```

```bash
# Sans les versions alpha et beta

$ npm show bootstrap@* version	# bash OK, zsh NOK
```



`"dependencies": {  "express": "2.9.1" }` (major.minor.patch)

- major version: 2
- minor version: 9
- patch version: 1



#### Installer un module:

Installer une <u>version spécifique</u>:

```bash
$ npm info @popperjs/core

@popperjs/core@2.9.2 | MIT | deps: none | versions: 62
Tooltip and Popover Positioning Engine
https://github.com/popperjs/popper-core#readme

dist-tags:
latest: 2.9.2

# Version spécifique (@x.y.z)
$ npm install @popperjs/core@2.8.0

$ npm ls
Downloads@ /Users/bruno/Downloads
└── @popperjs/core@2.8.0
```

Installer <u>latest minor version</u>:

```bash
$ npm info @popperjs/core

@popperjs/core@2.9.2 | MIT | deps: none | versions: 62
Tooltip and Popover Positioning Engine
https://github.com/popperjs/popper-core#readme

dist-tags:
latest: 2.9.2

# Latest minor version (^)
# Update to 2.*.*
$ npm install @popperjs/core@^2.8.0

$ npm ls
Downloads@ /Users/bruno/Downloads
└── @popperjs/core@2.9.2
```

Installer <u>latest patch version</u>:

```bash
$ npm info @popperjs/core

@popperjs/core@2.9.2 | MIT | deps: none | versions: 62
Tooltip and Popover Positioning Engine
https://github.com/popperjs/popper-core#readme

dist-tags:
latest: 2.9.2

# Latest patch version (~)
# Update to 2.8.*
$ npm install @popperjs/core@~2.8.0

$ npm ls
Downloads@ /Users/bruno/Downloads
└── @popperjs/core@2.8.6
```

Installer la <u>dernière version</u>:

```bash
$ npm info @popperjs/core

@popperjs/core@2.9.2 | MIT | deps: none | versions: 62
Tooltip and Popover Positioning Engine
https://github.com/popperjs/popper-core#readme

dist-tags:
latest: 2.9.2

# Dernière version (@latest)
$ npm install @popperjs/core@latest

$npm ls
Downloads@ /Users/bruno/Downloads
└── @popperjs/core@2.9.2
```



#### Supprimer tous les modules:

```bash
$ for package in `ls node_modules`; do npm uninstall $package; done;
```



#### Maintenance:

```bash
$ npm doctor
npm WARN verifyCachedFiles Content garbage-collected: 46 (4358853 bytes)
npm WARN verifyCachedFiles Cache issues have been fixed
Check                               Value                        Recommendation
npm ping                            OK
npm -v                              v5.10.0                      Use npm v6.4.1
node -v                             v8.11.4
npm config get registry             https://registry.npmjs.org/
which git                           /usr/bin/git
Perms check on cached files         ok
Perms check on global node_modules  ok
Perms check on local node_modules   ok
Verify cache contents               verified 2159 tarballs
```



#### Info sur un package:

```bash
$ npm show cli

# idem:
# npm info cli
# npm view cli

cli@1.0.1 | MIT | deps: 2 | versions: 59
A tool for rapidly building command line apps
http://github.com/node-js-libs/cli

keywords: cli, command line, opts, parseopt, opt, args, console, argsparse, optparse, autocomplete, command, autocompletion

dist
.tarball: https://registry.npmjs.org/cli/-/cli-1.0.1.tgz
.shasum: 22817534f24bfa4950c34d532d48ecbc621b8c14

dependencies:
exit: 0.1.2  glob: ^7.1.1

maintainers:
- cohara87 <cohara87@gmail.com>

dist-tags:
latest: 1.0.1

published over a year ago by cohara87 <cohara87@gmail.com>
```



#### Version d'un package installé:

```bash
$ npm list bootstrap
silverbook.local@1.0.0 /Users/bruno/Sites
└── bootstrap@5.0.1
```



#### Dernière version d'un package:

```bash
$ npm view bootstrap version
5.0.1
```



#### Rechercher un package:

[npms.io](https://npms.io)

[Libraries.io](https://libraries.io/search?platforms=NPM)

```bash
$ npm search tar
NAME                      | DESCRIPTION          | AUTHOR          | DATE       | VERSION  | KEYWORDS
tar                       | tar for node         | =nlf…           | 2020-08-14 | 6.0.5    |
tar-fs                    | filesystem bindings… | =mafintosh…     | 2020-11-06 | 2.1.1    | tar fs file tarball directory stream
tar-stream                | tar-stream is a…     | =mafintosh…     | 2020-09-10 | 2.1.4    | tar tarball parse parser generate generator stream stream2 streams streams2 str
archiver                  | a streaming…         | =ctalkington    | 2020-09-11 | 5.0.2    | archive archiver stream zip tar
```



#### Funding:

```bash
2 packages are looking for funding
  run `npm fund` for details
```

Ce message apparait généralement après la m-à-j d'un package tel Angular, React, Vue...
Il demande de lancer la commande `npm fund` pour lister les packages qui demandent des fonds pour continuer leur développement.

```bash
~/Sites/node_modules master* ⇡
❯ npm fund
/Users/bruno/Sites
├─┬ https://opencollective.com/bootstrap
│ └── bootstrap@4.5.2
└─┬ https://opencollective.com/popperjs
  └── popper.js@1.16.1
```

Pour éviter ce message:

```bash
# à l'installation:
npm install --no-fund <package>  

# configuration globale
npm config set fund false --global

# configuration par package:
npm config set fund false 


```



#### Erreur *"No matching version found for tar@^6.0.5"*:

```bash
$ npm install -g --unsafe-perm homebridge-config-ui-x
npm ERR! code ETARGET
npm ERR! notarget No matching version found for tar@^6.0.5.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
```

```bash
$ npm info tar

tar@6.0.5 | ISC | deps: 6 | versions: 88
tar for node
https://github.com/npm/node-tar#readme

dist
.tarball: https://registry.npmjs.org/tar/-/tar-6.0.5.tgz
```

```bash
$ npm install https://github.com/npm/node-tar/tarball/v6.0.5 --save
npm ERR! code ETARGET
npm ERR! notarget No matching version found for minizlib@^2.1.1.
```

```bash
$ npm info minizlib

minizlib@2.1.2 | MIT | deps: 2 | versions: 20
A small fast zlib stream built on [minipass](http://npm.im/minipass) and Node.js's zlib binding.
https://github.com/isaacs/minizlib#readme
```

```bash
$ npm install https://github.com/isaacs/minizlib/tarball/v2.1.1 --save
```

```bash
$ npm install https://github.com/npm/node-tar/tarball/v6.0.5 --save
```

```bash
$ npm install -g --unsafe-perm homebridge-config-ui-x

added 362 packages, and audited 362 packages in 45s
```

Autre solution:

```bash
$ npm cache clean --force
```



#### Erreur "npm ERR! 404  'deepl-cli' is not in this registry."

```bash
$ npm view deepl-cli
npm ERR! code E404
npm ERR! 404 Unpublished on 2023-12-27T16:56:29.470Z
npm ERR! 404
npm ERR! 404  'deepl-cli' is not in this registry.
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.
```

##### npm outdated --location=global --long 

```bash
npm ERR! code ENOVERSIONS
npm ERR! No versions available for deepl-cli
```

Le paquet n'est plus disponible sur https://registry.npmjs.org/

```bash
~/.nvm/versions/node/v20.9.0/lib/node_modules                                   
$ rm -rf deepl-cli 
```



#### Packages:

##### uninstall-all-modules

Desinstaller tous les modules=

```bash
$ npm uninstall
```

##### livephotoskit, jquery, picturefill