NPM

Install Node/Npm

On linux : yum/apt install nodejs
Important points about node install on Linux :
– npm is also installed when nodejs is installed.

during the install, a /usr/bin/npm symbolic link is added and that points to ../lib/node_modules/npm/bin/npm-cli.js that is an executable js file !
In fact, that is file executed by node as shown by the shebang line : #!/usr/bin/env node.

Non-root/sudoer users cannot install global package/libraries with npm (such as npm install -g foo-package). But we could mitigate it.  
npm will execute in both cases but when  we specify  global flag, it will have a distinct behavior.
For example executing npm root -g as sudoer/root or as no sudoer/no root will return a distinct path.
Using root as developer is not a good thing. Fortunately, we could still configure a directory for global resources of the current user thanks to the prefix property of npm.
To achieve that, as a regular user :
– create the dir where stored global resources for the user : mkdir -p ~/.npm-packages/lib – set the prefix property with as value the expected directory : npm config set prefix ~/.npm-packages
– update the ~/.bashrc to have the npm binaries (ng or any other program) in the user PATH and to have the man documentation such as :

NPM_PACKAGES="${HOME}/.npm-packages"
export PATH="$PATH:$NPM_PACKAGES/bin"
# Preserve MANPATH if you already defined it somewhere in your config.
# Otherwise, fall back to `manpath` so we can inherit from `/etc/manpath`.
export MANPATH="${MANPATH-$(manpath)}:$NPM_PACKAGES/share/man"

– reload it of course : . ~/.bashrc

Global flags

-v: --version
-h, -?, --help, -H: --usage
-s, --silent: --loglevel silent
-q, --quiet: --loglevel warn
-d: --loglevel info
-dd, --verbose: --loglevel verbose
-ddd: --loglevel silly
-g: --global
-C: --prefix
-l: --long
-m: --message
-p, --porcelain: --parseable
-reg: --registry
-f: --force
-desc: --description
-S: --save
-P: --save-prod
-D: --save-dev
-O: --save-optional
-B: --save-bundle
-E: --save-exact
-y: --yes
-n: --yes false
-- ll and la commands: ls --long

Commands

Command related to directory/path configuration

Change globally the default folder for npm cache :
npm config set cache C:\workspace\npm-cache --global

Common commands

Get a property :
npm config get key
Ex: get the global directory :
npm config get prefix

Set a property :
npm config set key value
Ex: set the global directory into ~/.npm-global :
npm config set prefix ‘~/.npm-global’

List (global) libs and theirs locations :
npm list (-g)

Install a package (globally) :
npm (-g) install packagename@version

List (global) outdated packages:
npm outdated (-g) 

Update (globally) a specific package
npm update -g <packagename>
Warn : for outdated packages outside of the semver range, update doesn’t work. We have to use npm install instead of.

Update all (global) packages
npm update -g

Display history of a package
npm view  <packagename>

Debug commands

Generate NPM dependency trees without the need of installing a dependency by using the command:
npm ls --all
You can get the dependency tree of a specific dependency like so:
npm ls [dependency]

You can also set the maximum depth level by doing:
npm ls –depth=[depth]

How to see logs from npm installation?
npm install ionic –loglevel verbose.

For permanent solution:
npm config edit command and add loglevel=verbose.
Now every npm command will show detailed logs

npm config

Description

npm gets its configuration values from the following sources, sorted by priority:

Command Line Flags:
--foo bar sets the foo configuration parameter to « bar ».
A -- argument tells the cli parser to stop reading flags.
Using --flag without specifying any value will set the value to true.

Example:
--flag1 --flag2 sets both configuration parameters to true
--flag1 --flag2 bar sets flag1 to true, and flag2 to bar.
--flag1 --flag2 -- bar sets both configuration parameters to true, and the bar is taken as a command argument.

Details

strict-peer-deps
Type: Boolean
If set to true, and --legacy-peer-deps is not set, then any conflicting peerDependencies will be treated as an install failure, even if npm could reasonably guess the appropriate resolution based on non-peer dependency relationships.

package.json dependencies

Source : https://docs.npmjs.com/files/package.json
~version : approximately equivalent to version
Update to all future patch versions, without incrementing the minor version.
Example: ~1.2.3 will use releases from 1.2.3 to <1.3.0

^version : « Compatible with version »
Update to all future minor/patch versions, without incrementing the major version.
Example: ~1.2.3 will use releases from 1.2.3 to <2.0.0

1.2.x : means 1.2.0, 1.2.1, etc., but not 1.3.0
version : must match version exactly
>version : must be greater than version
>=version : …
<<version : …
<=version : …
http://sometarurl : URL of a tar which will be downloaded and installed locally
* : matches any version
latest : Obtains latest release

package-lock.json file

It stores an exact, versioned dependency tree rather than using starred versioning like package.json itself (e.g. 1.0.*). This means you can guarantee the dependencies for other developers or prod releases, etc. It also has a mechanism to lock the tree but generally will regenerate if package.json changes.

Execute npm commands from package.json

Supposing we have this « scripts » attributes:

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "watch": "ng build --watch --configuration local",
    "test": "jest --verbose",
    "test-report": "jest --ci --reporters=default --reporters=jest-junit --passWithNoTests"
  },

We can execute script such as:
npm run start
npm run build
npm run test-report

We can also execute a script by adding some additional parameters in this way:
npm run test-report -- --runInBand --verbose

Common angular problems

Cannot find module ‘webpack’ – Angular
Delete these files/ folders (from your Angular root folder):
package-lock.json (Not the package.json)
/node_modules folder
/dist folder
Execute command (regenerate the package-lock.json and the /node_modules):
npm install

Random issue to download dependencies during npm install
package-lock.json maybe so cause if the referenced URL in « resolved » attribute is not correct.

Solution:
You can delete the attribute for the dependency where you encounter the problem.

If you need to automate the processing and you have many lines to delete,you can use this command:
sed -i '/"resolved"/d' package-lock.json
sed -i '/"integrity"/d' package-lock.json
sed -i -z 's/,\n\s*}/\n}/g' package-lock.json

Problem to execute npm install with a corporate registry

Solution:

curl -kL -u ${ARTIFACTORY_USER}:${ARTIFACTORY_PASSWORD} ${ARTIFACTORY_URL}/api/npm/auth/ > .npmrc
npm set strict-ssl false
npm config set registry ${ARTIFACTORY_URL}/api/npm/bar-project-npm/
npm set sass-binary-site "https://${ARTIFACTORY_USER}:${ARTIFACTORY_PASSWORD}@repo.artifactory.myfoocompany/artifactory/node-sass"
npm whoami
npm config ls -l

Increase or decrease the memory usable by node
–max_old_space_size=1024: Limit to 1 Go the memory usable.
Beware too few memory may make node to be not able to accomplish the task.

Ce contenu a été publié dans Non classé. Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *