Referensi Perintah npm / yarn / pnpm
Referensi cepat perintah npm, yarn, dan pnpm.
| Perintah (npm) | Deskripsi | |
|---|---|---|
npm init | Initialize a new project with a package.json file | |
npm init -y | Initialize with defaults (skip prompts) | |
npm install | Install all dependencies from package.json | |
npm ci | Clean install from lockfile (CI environments) | |
npm ls | List installed packages | |
npm outdated | Check 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 update | Update all packages to latest allowed versions | |
npm audit | Run a security audit | |
npm audit fix | Automatically fix vulnerabilities | |
npm dedupe | Reduce duplication in node_modules | |
npm cache clean --force | Clear the package cache | |
npm run <script> | Run a script defined in package.json | |
npm start | Run the 'start' script | |
npm test | Run the 'test' script | |
npm run build | Run the 'build' script | |
npx <cmd> | Execute a package binary without installing | |
npm login | Log in to npm registry | |
npm publish | Publish package to the registry | |
npm version <type> | Bump version (patch/minor/major) | |
npm pack | Create a tarball of the package | |
npm deprecate | Deprecate a published version |
Tentang alat ini
npm, yarn, dan pnpm adalah manajer paket paling banyak digunakan untuk JavaScript, masing-masing dengan sintaks perintah dan keunikan tersendiri. Pengembang yang bekerja di proyek berbeda sering beralih di antara mereka, dan mengingat perintah yang tepat untuk tugas tertentu dapat membuat frustrasi tanpa referensi yang jelas.
Referensi ini mengumpulkan perintah paling umum untuk memasang, memperbarui, menghapus, dan mendaftar paket di ketiga manajer sehingga Anda dapat menemukan pemanggilan yang tepat tanpa berkonsultasi dengan dokumentasi resmi setiap kali. Gunakan untuk memeriksa cara memasang ketergantungan pengembangan, menjalankan skrip khusus, mendaftar paket global, atau mengosongkan cache Anda sebelum mengatasi masalah pembangunan.
Setiap perintah disajikan berdampingan di npm, yarn, dan pnpm sehingga Anda dapat dengan cepat menyesuaikan alur kerja saat beralih alat. Baik Anda memulai proyek baru, mengelola dependensi workspace, atau men-debug paket yang hilang, halaman ini membantu Anda mendapatkan sintaks dengan benar sejak pertama kali.
Pertanyaan yang Sering Diajukan
Implementasi Kode
# 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.