Cài Đặt
Cài Đặt npm & Node
node -v && npm -v # check installed versions npm install -g npm@latest # update npm itself nvm install --lts # install Node LTS via nvm nvm use 20 # switch to Node 20
Lệnh Cài Đặt
npm installCài tất cả dependencies từ package.json
npm install pkgThêm package vào dependency
npm install -D pkgThêm package vào devDependency
npm install -g pkgCài package toàn cục
npm install [email protected]Cài phiên bản cụ thể
npm ciCài sạch từ lock file (CI/CD)
npm uninstall pkgGỡ bỏ package
Quản Lý Package
Quản Lý Packages
npm ls # list installed packages npm ls --depth=0 # top-level only npm outdated # check for newer versions npm update # update within semver range npm audit # check for vulnerabilities
Lệnh Quản Lý
npm lsLiệt kê packages đã cài dạng cây
npm outdatedHiển thị packages có phiên bản mới hơn
npm update [pkg]Cập nhật packages trong phạm vi semver
npm auditKiểm tra lỗ hổng bảo mật trong dependencies
npm audit fixTự động sửa lỗ hổng
npm pruneXóa các packages không cần thiết
npm dedupeLàm phẳng dependency tree để giảm trùng lặp
Scripts
Chạy Scripts
npm run build # run "build" script npm test # shortcut for "test" script npm start # shortcut for "start" script npm run lint -- --fix # pass args to script npm run dev & # run in background
Script Lifecycle
npm test / npm tChạy scripts.test
npm startChạy scripts.start
npm run <name>Chạy bất kỳ script tùy chỉnh nào
pre<name>Tự động chạy trước
post<name>Tự động chạy sau
npm runLiệt kê tất cả scripts có sẵn
package.json
Khởi Tạo & Trường
npm init # interactive setup npm init -y # accept all defaults npm pkg set name="my-app" # set a field npm pkg get version # read a field
Trường Quan Trọng
nameTên package (chữ thường, không dấu cách)
versionPhiên bản hiện tại (semver: major.minor.patch)
mainEntry point cho CommonJS (require)
moduleEntry point cho ES modules (bundlers)
type"module" cho ESM, "commonjs" cho CJS (mặc định)
scriptsLệnh được đặt tên (build, test, start, v.v.)
dependenciesDependencies sản xuất
devDependenciesDependencies chỉ dùng khi phát triển
enginesYêu cầu phiên bản Node/npm
Versioning
Lệnh Version
npm version patch # 1.0.0 → 1.0.1 npm version minor # 1.0.1 → 1.1.0 npm version major # 1.1.0 → 2.0.0 npm version 3.2.1 # set explicit version npm version prerelease --preid=beta # 1.0.0-beta.0
Khoảng Semver
^1.2.3Tương thích: >=1.2.3 <2.0.0 (mặc định)
~1.2.3Patch-level: >=1.2.3 <1.3.0
1.2.3Chỉ phiên bản chính xác này
>=1.0.0 <2.0.0Khoảng tường minh
*Bất kỳ phiên bản nào
1.x / 1.2.xKhoảng wildcard
latestTag phiên bản mới nhất đã publish
Publishing
Quy Trình Publish
npm login # authenticate to registry npm publish # publish public package npm publish --access public # scoped package as public npm unpublish [email protected] # remove specific version npm deprecate pkg@"<2" "Use v2+" # deprecate old versions
Tham Khảo Publish
npm loginXác thực với npm registry
npm publishPublish package lên registry
npm packTạo tarball mà không publish
npm unpublishXóa phiên bản đã publish (trong 72h)
npm deprecateĐánh dấu các phiên bản là deprecated
.npmignoreFile cần loại trừ khỏi package đã publish
files (package.json)Danh sách file được phép đưa vào package
Workspaces
Lệnh Workspace
npm init -w packages/core # create workspace npm install -w packages/core lodash # install in workspace npm run build --workspaces # run in all workspaces npm run test -w packages/api # run in specific workspace npm ls --workspaces # list workspace deps
Cấu Hình Workspace
workspaces (package.json)Mảng workspace globs: ["packages/*"]
-w / --workspaceNhắm đến workspace cụ thể
--workspacesChạy lệnh trên tất cả workspaces
--include-workspace-rootBao gồm package root trong workspace
npm install (root)Cài tất cả workspace dependencies
HoistingShared deps được nâng lên root node_modules
npx
Chạy Với npx
npx create-react-app my-app # run without installing npx tsc --init # run local or remote bin npx -p typescript tsc file.ts # specify package explicitly npx --yes create-next-app # skip install prompt npx node@18 -e "console.log('hi')" # run with specific Node
Tùy Chọn npx
npx cmdChạy cmd từ node_modules/.bin hoặc remote
npx -p pkg cmdCài pkg, rồi chạy cmd
npx --yes cmdTự xác nhận cài đặt
npx --no cmdTừ chối cài đặt — thất bại nếu không có local
npx -c 'cmd'Chạy shell command với PATH của npx
npx node@verChạy phiên bản Node.js cụ thể
Cấu Hình
Lệnh Cấu Hình
npm config list # show current config npm config set registry https://r.npmjs.com/ npm config set init-author-name "Name" npm config get prefix # global install path npm config delete key # remove a config value
Tham Khảo Cấu Hình
.npmrc (project)File cấu hình theo project
~/.npmrcFile cấu hình theo user
registryURL của package registry
save-exacttrue để ghim phiên bản chính xác khi cài
engine-stricttrue để bắt buộc trường engines
fundfalse để ẩn thông báo tài trợ
auditfalse để bỏ qua audit khi cài
Mẫu Phổ Biến
One-Liners
npm ls --depth=0 --json | jq '.dependencies | keys[]' npm outdated --long # show type and homepage npm cache clean --force # clear npm cache npm explain pkg # why is pkg installed? npm exec -- envinfo --system # system info for bug reports
Công Thức Hữu Dụng
Chỉ lock filenpm ci — cài sạch từ package-lock.json
Kiểm tra licensenpx license-checker --summary
Tìm deps không dùngnpx depcheck
Kích thước bundlenpx bundlephobia-cli pkg — kiểm tra kích thước
Nâng cấp tất cảnpx npm-check-updates -u && npm install
Registry nội bộnpx verdaccio — chạy private registry