w3resource

Npm-install, npm-install-ci-test and npm-install-test cli commands


In the previous tutorial we looked at npm's hook and init commands, in this tutorial we will examine how npm-install, npm-install-ci-test and npm-install-test commands.

Npm-install

The npm install command is used to install packages.

Synopsis

npm install (without args, in package dir)
npm install [<@scope>/]<name>
npm install [<@scope>/]<name>@<tag>
npm install [<@scope>/]<name>@<version>
npm install [<@scope>/]<name>@<version range>
npm install <git-host>:<git-user>/<repo-name>
npm install <git repo url>
npm install <tarball file>
npm install <tarball url>
npm install <folder>

alias: npm i
common options: [-P|--save-prod|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [-B|--save-bundle] [--no-save] [--dry-run]

Description

This command will install a package, and all the packages that the package depends on. In the case where the package contains a package-lock or shrinkwrap file, it will control the installation of dependencies, however the npm-shrinkwrap.json will take precedence if both files exist.

  • npm install (in package directory, no argument):
  • This command will install all the dependencies found in the local node_modules folder, if you set your NODE_ENV environment variable to production, it will not include the devDependencies in the installation.

  • npm install <folder>:
  • This command will install the package in the directory as a symlink in the current project. The dependencies will be installed before it is linked. In the case where <folder> sits inside the root of your project, its dependencies can be hoisted to the toplevel node_modules as they would for other types of dependencies.

  • npm install <tarball file>:
  • This will install a package that is sitting on the filesystem. To link a dev directory into your npm root, you can easily do this, using npm link.

  • npm install <tarball url>:
  • This command fetches the tarball url, and then installs it. To distinguish between this and other options, the argument has to start with "http://" or "https://"

     npm install https://github.com/indexzero/forever/tarball/v0.5.6
  • npm install [<@scope>/] <name>
  • This command will perform a <name>@<tag> install, where <tag> represents the tag config.

    In most of the cases, this installs the version of the modules tagged as latest on the npm registry.

    npm install sax
  • npm install [<@scope>/] <name>@<tag>:
  • This command will install the version of the package that is referenced by the specified tag. If there is no existing tag in the package's registry data, the command will fail.

    Here is an example:
    npm install sax@latest
        npm install @myorg/mypackage@latest
  • npm install [<@scope>/] <name>@<version>:
  • This command installs a version of the package that matches the specified version range. It will follow the same rules required to resolve dependencies described in the package.json.

    An example is shown below:

    npm install sax@">=0.1.0 <0.2.0"
       npm install @myorg/privatepackage@">=0.1.0 <0.2.0"
  • npm install <git remote url>
  • This will install the package from the hosted git provider, it clones the url with git. When you specify a full git remote url, only that url is attempted.

    <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]

The accepted protocols are: git, git+ssh, git+http, git+https, and git+file

Here is a list of git environment variables that are recognized by npm and will be added to the environment when running git:

o	GIT_ASKPASS
o	GIT_EXEC_PATH
o	GIT_PROXY_COMMAND
o	GIT_SSH
o	GIT_SSH_COMMAND
o	GIT_SSL_CAINFO
o	GIT_SSL_NO_VERIFY

Here is an example:

npm install git+ssh://[email protected]:npm/cli.git#v1.0.27
    npm install git+ssh://[email protected]:npm/cli#semver:^5.0
    npm install git+https://[email protected]/npm/cli.git
    npm install git://github.com/npm/cli.git#v1.0.27
    GIT_SSH_COMMAND='ssh -i ~/.ssh/custom_ident' npm install git+ssh://[email protected]:npm/cli.git
  • npm install <githubname>/<githubrepo>[#<commit-ish>]:
  • npm install github:<githubname>/<githubrepo>[#<commit-ish>]:
  • This will install the package at https://github.com/githubname/githubrepo by attempting to clone it using git.

In the case where #<commit-ish> is provided, it will be used to clone exactly that commit. if your commit-ish has the format #semver:<semver>, then <semver> can be any valid semver range or exact version, then npm will look for any tags or refs that matches that range in the remote repository, in the same way it would for a registry dependency.

  • Npm install bitbucket:<bitbucketname>/<bitbucketrepo>[#<commit-ish>]:
  • This will install the package at https://bitbucket.org/bitbucketname/bitbucketrepo by attempting to clone it using git. If you provide #<commit-ish>, it is used to clone exactly that commit. Here is an example:
     npm install bitbucket:mybitbucketuser/myproject
  • npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]:
  • This command will install the package at https://gitlab.com/gitlabname/gitlabrepo by attempting to clone it using git. An example is shown below:

    npm install gitlab:mygitlabuser/myproject
        npm install gitlab:myusr/myproj#semver:^5.0

ALGORITHM

Npm uses the following algorithm to install a package:

load the existing node_modules tree from disk
clone the tree
fetch the package.json and assorted metadata and add it to the clone
walk the clone and add any missing dependencies
  dependencies will be added as close to the top as is possible
  without breaking any other modules
compare the original tree with the cloned tree and make a list of
actions to take to convert one to the other
execute all of the actions, deepest first
  kinds of actions are install, update, remove and move

The limitations of npm's install Algorithm

npm will always refuse to install any package with an identical name to the current package. You can overwrite this with the -force flag, however, in most cases you can address this by changing the name of the local package.

Npm install-ci-test

This command is used to install a project with a clean slate and run tests.

Synopsis

npm install-ci-test
alias: npm cit

Description

This command is run as npm ci followed immediately followed by an npm test.

Npm install-test

This will install package(s) and run tests.

Synopsis

npm install-test (with no args, in package dir)
npm install-test [<@scope>/]<name>
npm install-test [<@scope>/]<name>@<tag>
npm install-test [<@scope>/]<name>@<version>
npm install-test [<@scope>/]<name>@<version range>
npm install-test <tarball file>
npm install-test <tarball url>
npm install-test <folder>

alias: npm it
common options: [--save|--save-dev|--save-optional] [--save-exact] [--dry-run]

Description

This command will be run as npm install followed immediately by an npm test. It takes the same arguments as npm install.

Previous: npm-hook and npm-init cli options
Next: npm-link and npm-logout cli option



Follow us on Facebook and Twitter for latest update.