0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Node.js完全攻略ガイド ~npmによるパッケージ管理~

Posted at

参考教材

npmによるパッケージ管理

npmとは?

Node.js上で使用するパッケージを管理するツール
コマンド一つでパッケージのインストール、削除、更新が可能

なぜnpmが必要なのか?

npmを使わない
外部からパッケージを持ってきて(または自分で書いて)から、操作する必要がある

npmを使う
npmインストールでパッケージをインストールできるので、呼び出すだけで使用できる

パッケージが読み込まれる流れ

// lodashはパッケージなのでNode.jsがnode.modulesから探してくる
// 先頭にpathの指定がない場合はパッケージとして取り扱われる
import _ from "lodash";

// 現在のカレントディレクトリからファイルを探す
import _ from "./lodash";

パッケージ名の指定はNode.js上でしか使えない

package.jsonの中身を見てみよう

dependenciesとdevDependenciesの違いについて学ぼう

dependenciesとdevDependenciesにパッケージがインストールされていれば、パッケージを読み込んで実行することが可能

Dependenciesの意味は依存
main.jsのコードが依存しているのがDependenciesの中の "is-odd" になる

2025-10-23 11.40の画像.jpeg

// 例
{
  "devDependencies": {
    "is-odd": "^3.0.1"
  }
}

devDependencies

devDependenciesのdevは、開発環境で依存しているパッケージという意味

dependencies

開発環境と本番環境両方に依存するパッケージという意味


基本的にnpmのパッケージは第三者にインストールされるものなので、ソースコードを動かすのに必要でないパッケージまでインストールされてしまうと、その分リスク容量をとってしまうので使い分けがされている

開発時に必要なパッケージはdevDependenciesでインストール
ソースコードに必要なのはdependencies内に記述する


package.jsonの"engines"と"files"と"bugs"について

ローカルパッケージとグローバルパッケージ

ターミナルから Live server を実行する方法

npx live-server

npx
ローカルパッケージを起動するnpmのコマンド


グローバルパッケージ
PC全体で利用できる

npm i -g live-server
live-server
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?