How to use npm install and npm ci

How to use npm install and npm ci

How to use the npm command.

npm init

これでプロジェクトが作成されます。

このフォルダ配下をnpm管理下にするということです。package.jsonが作成されると思います。

次にツール(パッケージ)を入れます。「gulp」を入れてみます。

npm install --save-dev gulp

A folder named “node_modules” will be created, under which gulp and all tools that depend on it will be installed.

npm install” can be abbreviated to “npm i”.

`–save-dev`

When you npm install, add “–save-dev” to mean “tool to be used only during development”.

After this installation, package.json will have changed.

As you can see, “gulp” is added to “devDependencies”, not “dependencies”.

Tools that are not needed in production are installed with “–save-dev”.

By the way, –save-dev can be shortened to -D. (The -D option is not as well known as you might think.)

uninstall

Uninstall with the following command

npm uninstall --save-dev gulp

-g

Adding “-g” will result in a global installation, i.e., installation is done as usual. The package.json is not changed.

npm install chai -g

An installation without “-g” is called a local installation.

In this case, too, uninstallation is simply changing “install” to “uninstall.

npm uninstall chai -g

Use the ls command to check the globally installed modules.

npm ls -g --depth=0

npm install

This command will look at the dependencies (dependencies and devDependencies) in package.json and install them all.

If you are developing as a team, dependencies in package.json are often added and will be subject to commit.

To install dependencies that someone else has added, you will need to run npm install.

Specify version and install

There are times when you do not want to install the latest version of a package.

In such cases, you can specify the version of the package to install.

npm install class-validator@0.9.0

If you say “@version”, you can install that version.

Here is a definition of the terms.

We often refer to each of 0.9.1 as follows We call it semantic versioning.

version designation
0 major version
9 minor version
1 Patch Version

Difference between ^(caret) and ~(tilde)

Since the major version 5 of npm, installing a package now defaults to ^ (caret).

If you write ~0.9.1, it allows the version to go up from 0.9.1 to 0.9.9.

If it says ^1.1.1, it allows the version to go up from 1.1.1 to 1.9.9.

I want to fix the version

When you npm install, the ^ (tilde) will appear on its own, but this assumes that “open source developers do not try to break compatibility with minor version upgrades”.

However, in fact, there are some modules that are not so.

So, there are cases where you want to fix the version.

package-lock.json has been added since npm ver5.x.x. This is also an additional function to fix the version.

By creating npm-shrinkwrap.json with the npm shrinkwrap command, the version will be fixed.

How to check npm version

You can check it with npm -v.

npm -v

You can see the packages with dependencies in a tree structure.

npm -l

How to use npm ci

When “npm install” is performed, there is a possibility that “node_modules” will be updated and package-lock.json will be changed.

This “node_modules” is a tricky one, and if it is updated, the behavior may often change, so it is better to “npm ci” than “npm install”.

For example, if you are new to the project, it is better to do npm ci than npm install from package.json.

The reasons are as follows

  • Remove “node_modules” before installation.
  • package-lock.json is not updated (recreate node_modules based on package-lock.json)
https://docs.npmjs.com/cli/ci

コメント

タイトルとURLをコピーしました