コンテンツへスキップ
🛠️ToolsShed

npm / yarn / pnpm コマンドリファレンス

npm、yarn、pnpmのパッケージマネージャーコマンドクイックリファレンス。

コマンド (npm)説明
npm initInitialize a new project with a package.json file
npm init -yInitialize with defaults (skip prompts)
npm installInstall all dependencies from package.json
npm ciClean install from lockfile (CI environments)
npm lsList installed packages
npm outdatedCheck for outdated packages
npm install <pkg>Install and add to dependencies
npm install -D <pkg>Install and add to devDependencies
npm install -g <pkg>Install package globally
npm uninstall <pkg>Remove a package
npm update <pkg>Update a specific package
npm updateUpdate all packages to latest allowed versions
npm auditRun a security audit
npm audit fixAutomatically fix vulnerabilities
npm dedupeReduce duplication in node_modules
npm cache clean --forceClear the package cache
npm run <script>Run a script defined in package.json
npm startRun the 'start' script
npm testRun the 'test' script
npm run buildRun the 'build' script
npx <cmd>Execute a package binary without installing
npm loginLog in to npm registry
npm publishPublish package to the registry
npm version <type>Bump version (patch/minor/major)
npm packCreate a tarball of the package
npm deprecateDeprecate a published version

このツールについて

npm、yarn、pnpmはJavaScriptで最も広く使われているパッケージマネージャーで、それぞれ独自のコマンド構文と特徴を持っています。異なるプロジェクトを行き来する開発者は、頻繁にこれらを使い分けますが、特定のタスク用に正確なコマンドを思い出すのは、明確なリファレンスなしでは苦労することがあります。

このリファレンスは、3つのマネージャーすべてでパッケージをインストール、更新、削除、一覧表示するための最も一般的なコマンドを集めており、公式ドキュメントを毎回参照しなくても正しいコマンドが見つかります。開発依存関係をインストールする方法、カスタムスクリプトを実行する方法、グローバルパッケージを一覧表示する方法、ビルド問題をトラブルシューティング前にキャッシュをクリアする方法などを確認するのに役立ちます。

各コマンドはnpm、yarn、pnpmの3つで横並びに表示されているので、ツール間を切り替えるときにワークフローを素早く適応させられます。新しいプロジェクトを開始するにせよ、ワークスペース依存関係を管理するにせよ、行方不明のパッケージをデバッグするにせよ、このページは構文を初回で正しく入力するのに役立ちます。

よくある質問

コード実装

# Run npm/shell commands from Python
import subprocess
import json
import os

def npm_install(package, dev=False):
    """Install an npm package"""
    cmd = ['npm', 'install']
    if dev:
        cmd.append('--save-dev')
    cmd.append(package)
    result = subprocess.run(cmd, capture_output=True, text=True)
    return result.returncode == 0

def get_package_json():
    """Read package.json"""
    with open('package.json') as f:
        return json.load(f)

def npm_run(script):
    """Run an npm script"""
    result = subprocess.run(
        ['npm', 'run', script],
        capture_output=True, text=True
    )
    print(result.stdout)
    if result.returncode != 0:
        print(result.stderr)
    return result.returncode == 0

# Example usage
npm_install('lodash')
npm_install('typescript', dev=True)
npm_run('build')

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.