FORSMILE
JA
開発記事2016/01/26

[Node.js] Installing Node.js and a Simple Explanation of npm Usage

This article explains how to install Node.js on Windows.

Back to Blog

This article explains how to install Node.js on Windows.

Reference URLs

1. DOWNLOAD THE FILE

Although v12 is an older version, it is still covered by Long-term Support.

Node versions change rapidly, but even-numbered versions are LTS and are supported for a longer period.

Odd-numbered versions have shorter support periods. For example, v14 is supported until 2023-4-30, while the next version, v15, is only supported until 2021-6-1.

It's good practice to include Node.js version management, but we'll omit it this time.

Double-click the downloaded file to start the installation.

2. CHECK WITH COMMAND PROMPT

After installing Node.js, a shortcut named 'Node.js command prompt' will also be created inside the node folder.

text
node -v

If you enter the above command and the Node version appears, it means the installation was successful.

text
npm -v

Running this command allows you to check the version of npm, which controls Node.js packages.

3. HOW TO USE NPM

You can install various Node.js packages.

By sharing package.json, developers can also share their development environments.

Initialize npm to use it in the current directory. A `package.json` file will be generated.

text
npm init -y

You can install a specified 'package name' in the current directory using the following command.

bash
npm install <package-name>
text
c:\project\test>

If the above is displayed, it means the package will be installed inside the `test` directory.

php
// package.json
{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "keywords": [],
  "description": ""
}
text
"keywords": [
    "node",
    "javascript",
    "express"
  ],

Installing Packages

bash
npm install <package-name>

Executing the above command allows you to install the specified 'package name'.

text
--save      Adds to dependencies in package.json
--save-dev  Adds to devDependencies in package.json
-g          Installs globally in the local environment
bash
npm install express --save

When you install a package, it is added to `package.json`.

Refer to the following example for packages. They are listed under `dependencies` and `devDependencies`.

css
{
  "name": "test",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.13.3"
  },
  "devDependencies" : {
    "gulp": "^3.8.10"
  }
}

・dependencies: Applications required for the production version.

・devDependencies: Applications used only in the development environment.

Uninstalling Packages

text
npm uninstall <package-name>

To also remove it from `package.json`, run the command with `--save`.

Updating Packages

bash
npm install <package-name>

Running `npm install <package-name>` again for an already installed package will update it.

sql
npm update

This command updates all installed packages.

text
npm outdated --depth=0

To Update npm Itself

To update npm itself, you also use `install`. In some cases, you might need administrator privileges for this (right-click 'Command Prompt' and select 'Run as administrator').

bash
npm install npm@latest -g

This has been a brief introduction to installing Node.js and npm.

text
node sample.js

You can run files like this. I don't use it very often, but it's good to know for reference.

📦
Amazon で関連書籍・ツールを検索
web development programming book
Amazonで探す →(アソシエイトリンク)