Nodejsとは

PHPz
リリース: 2024-08-01 01:44:23
オリジナル
806 人が閲覧しました

What is Nodejs

Nodejs は JavaScript ランタイムです。これは、nodejs がブラウザの外部で JavaScript を実行できるようにするプログラムであることを意味します。その結果、nodejs を使用してバックエンド アプリケーションを開発できるようになります。さて、これはバックエンドに限定されません。いくつか例を挙げると、デスクトップ アプリケーション、IoT、クラウド アプリケーションを構築できます。 Nodejs はクロスプラットフォームです。プログラム自体は Linux、Windows、macOS で実行されます。

Nodejs を使用する理由

Nodejs には以下のような利点がありますが、これらに限定されません。

  • ノンブロッキング I/O
  • 非同期
  • スケーラブル
  • イベント運営
  • 遅延が少ない
  • スレあり
  • いつでもどこでも使用可能
  • 大規模なコミュニティを持つ

ことわざにあるように、すぐに返品すると長期的には不便が生じます。ここでの欠点は、JavaScript (ちなみに私は JavaScript が大好きです) と、スケーリングを念頭に置いて構築したいシステムを設計できない場合があることです。繰り返しますが、Nodejs ではなく、Nodejs を使用するツールと人間です。

ここでnodejs について詳しく読むことができます

インストール

Nodejs の人々は賢いので、それを尊重します。彼らのおかげで、あなたも私もインストールが簡単になりました。技術的な知識のない人でも、Nodejs をセットアップしてコードを書き始めることができます。彼らは、使用できるオプションを提供しました:

  • パッケージマネージャー
  • 事前に構築されたインストーラー
  • 事前に構築されたバイナリ
  • ソース コードをビルドしてインストール

このうち、最初の 3 つは友好的です。したがって、それらのいずれかを選択してください。 download-nodejs に進み、「nodejs をそこに入れましょう」

現時点で、現在のノードのバージョンは 22、LTS (長期サポートあり) は 20 です。

私は Linux マシンを使用しているので、nvm (ノード バージョン マネージャー) を使用してインストールします。これにより、nodejs の複数のバージョンを使用できることがわかります。これは、macOS でもそのまま使用できます。

# installs nvm (Node Version Manager)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

# download and install Node.js (you may need to restart the terminal)
nvm install 20

# verifies the right Node.js version is in the environment
node -v # should print `v20.15.1`

# verifies the right npm version is in the environment
npm -v # should print `10.7.0`
ログイン後にコピー

これは、nodejs プラットフォーム (Web サイト) 上の同じスクリプトです。したがって、これらのコマンドを実行しても問題はありません。

Windows の場合も同様です

# installs fnm (Fast Node Manager)
winget install Schniz.fnm

# download and install Node.js
fnm use --install-if-missing 20

# verifies the right Node.js version is in the environment
node -v # should print `v20.15.1`

# verifies the right npm version is in the environment
npm -v # should print `10.7.0`
ログイン後にコピー

または、事前に構築されたインストール、node-prebuilt-installer をダウンロードするだけです。最終的には、最後の 2 つのコマンドを実行してインストールを確認できるようになります。

# verifies the right Node.js version is in the environment
node -v # should print `v20.15.1`

# verifies the right npm version is in the environment
npm -v # should print `10.7.0`
ログイン後にコピー

NVM

nvm はインストール中に Windows のオプションではありませんでしたが、ここでインストールできます。nvm について少し知っておくと勉強になります。

nvm list コマンドを使用して、所有している他のバージョンの Nodejs をすべてリストします

username@computer-name:~$ nvm list
->     v18.18.0
default -> 18.18.0 (-> v18.18.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.18.0) (default)
stable -> 18.18 (-> v18.18.0) (default)
lts/* -> lts/hydrogen (-> v18.18.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.18.0
ログイン後にコピー

上記のことから、v18.18.0 が私が実行している Nodejs であることがわかります。

nvm install 20 を使用して、20 LTS などの他のバージョンをインストールできます

username@computer-name:~$ nvm install 20
Downloading and installing node v20.15.1...
Downloading https://nodejs.org/dist/v20.15.1/node-v20.15.1-linux-x64.tar.xz...
######################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.15.1 (npm v10.7.0)
ログイン後にコピー

これは自動的に v20.15.1 に切り替わりました。最新のLTSです。

これで、nvm use 18 により、希望するノードのバージョンに切り替えることができます

username@computer-name:~$ nvm use 18
Now using node v18.18.0 (npm v10.8.2)
username@computer-name:~$
username@computer-name:~$ node -v
v18.18.0
ログイン後にコピー

nvm については以上です

故宮

npm はノード パッケージ マネージャーです。パッケージが何なのか疑問に思っても、心配する必要はありません。パッケージはライブラリと同じです。他の人が書いた一部のコード スニペットやプログラムは、私たちのプログラムで何かを行うために使用できます。したがって、パッケージは問題などを解決することを目的としています。 npm と、yarn、pnpm、bun などの他のノード パッケージ マネージャーは、プロジェクト用にインストールするパッケージの管理に役立ちます。ここでは npm のみに焦点を当てます。

(JavaScript だけでなく)nodejs プロジェクトを開始するには、node パッケージを使用する必要があります。つまり、サードパーティのライブラリ (自分で作成したものではなく、Nodejs に付属していたプログラム) に依存せずにプログラム全体を開発する場合もあります。

コマンド npm init を使用してノード package.json ファイルを作成することで、nodejs アプリケーションを作成できます。 npm init --help を実行すると、npm init の詳細が表示されます。通常、ノード プログラムは新しい環境 (フォルダー) で起動することをお勧めします。したがって、それを作成し、helloworld という名前を付けます。ターミナルを使用します。

username@computer-name:~$ mkdir helloworld
username@computer-name:~$ cd helloworld/
username@computer-name:~/helloworld$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (helloworld) 
version: (1.0.0) 
description: 
entry point: (index.js) 
test command: 
git repository: 
keywords: 
author: 
license: (ISC) 
About to write to /home/username/helloworld/package.json:

{
  "name": "helloworld",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
 },
  "author": "",
  "license": "ISC",
  "description": ""
}

Is this OK? (yes) 

username@computer-name:~/helloworld$ 
ログイン後にコピー
  • mkdir helloworld というフォルダーを作成しました
  • helloworld フォルダーに変更し、cd helloworld
  • 次に、ノードを初期化します (npm init)

これはインストール ウィザードのようなもので、設定手順を順を追って説明します。後で更新できることに注意してください。プロセス全体が終了するまで、ENTER、ENTER を押すだけです。ファイル エクスプローラーで helloworld フォルダーを開くと、上記の出力と同様の内容を含む新しいファイル package.json が表示されます。

{
  "name": "helloworld",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
 },
  "author": "",
  "license": "ISC",
  "description": ""
}
ログイン後にコピー

この構成は直感的です。これから作成するプロジェクト (またはプログラム) の名前が表示されます。親フォルダー名をプロジェクト名として使用します。ノード (プロジェクト) の初期化プロセス中に、ノードに名前を付けたり、他のフィールドに値を指定したりすることもできます。これは、ENTER、ENTER、…を押していた場所です

Another way to run through this without hitting ENTER, ENTER, …, is to do, npm init -y . -y, mean, yes, use the default values.

Primarily, node packages are on npmjs.com. Let’s say we want to install the expressjs library. This is how to search for express on npmjs. The docs will tell you how to install it using the command, npm i express.

username@computer-name:~/helloworld$ npm i express

added 64 packages, and audited 65 packages in 4s

12 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
ログイン後にコピー

i means install. You write it out as npm install express. The package.json will be updated with the package added.

{
  "name": "helloworld",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
 },
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "express": "^4.19.2"
 }
}

ログイン後にコピー

Now, we have a new dependency.

Note that no file or folder will be created. When we do ls

username@computer-name:~/helloworld$ ls
node_modules  package.json  package-lock.json
ログイン後にコピー
  • We have node_modules, which is a folder that holds the dependencies (packages) our program will use.
  • Also we have, package-lock.json, which serves as a lockfile, hence the name. It captures the exact versions of the package that we install and use to create our programs. This way, the same packages and their specific versions can be used all the time since different versions of the same package may behave differently.

Anyways, we can install packages in three ways or rather environment. This is basically where you want the package to be used.

  • global: This will be accessible to all node programs you have. Usually, install packages globally when they are general purpose programs like command line utilities.
  • development: This is meant for development only and not used on some remote servers since the remote server will have its way of handling the use case of that dependency. These are usually utility libraries that work with other libraries to achieve a purpose. These may include but are not limited to eslint, prettier, dotenv, etc.
  • production: This is a package that our application primarily relies on to function. Like express.

We can do,

  • npm i -g package-names ... to globally install a package
  • npm i --global package-names ... to globally install a package
  • npm i -S package-names ... to install a package (for production)
  • npm i --save package-names ... to install a package (for production)
  • npm i -D package-names ... to install a package (for development, you won’t need it to make our application run)
  • npm i --save-dev package-names ... to install a package (for development, you won’t need it to make our application run)
  • npm uninstall package-names ... to remove or uninstall package

Essentially this is all that we will need to manage our packages.

以上がNodejsとはの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!