กลับไปหน้าสูตร
#npm#nodejs#package-manager#cheatsheet
npm Command Cheatsheet
รวมคำสั่ง npm แบบจัดเต็ม: install, scripts, workspace, audit, cache, publish, troubleshooting และ CI usage
11 มีนาคม 2569อ่านประมาณ 2 นาที
Basics
| Command | ใช้ทำอะไร |
|---|---|
npm -v | ดูเวอร์ชัน npm |
npm init -y | สร้าง package.json |
npm install | ติดตั้ง dependencies |
npm ci | ติดตั้งแบบ lockfile strict (CI) |
npm outdated | ดูแพ็กเกจที่ outdated |
npm update | อัปเดตตาม semver range |
Install / Remove
| Command | ใช้ทำอะไร |
|---|---|
npm i <pkg> | ติดตั้ง dependency |
npm i -D <pkg> | ติดตั้ง devDependency |
npm i -g <pkg> | ติดตั้ง global |
npm uninstall <pkg> | ลบ dependency |
npm uninstall -D <pkg> | ลบ devDependency |
Scripts
| Command | ใช้ทำอะไร |
|---|---|
npm run <script> | รัน script |
npm run lint -- --fix | ส่ง args เพิ่มให้ script |
npm test | รัน test |
npm run build | build project |
npm run dev | run dev server |
Workspace
| Command | ใช้ทำอะไร |
|---|---|
npm -w <workspace> run dev | รัน script เฉพาะ workspace |
npm run -ws lint | รัน lint ทุก workspace |
npm i -w <workspace> <pkg> | install package ใน workspace |
npm ls -w <workspace> | ดู tree dependencies |
Audit / Security
| Command | ใช้ทำอะไร |
|---|---|
npm audit | สแกนช่องโหว่ |
npm audit fix | แก้ที่แก้ได้อัตโนมัติ |
npm audit fix --force | บังคับแก้ (เสี่ยง breaking) |
Cache / Debug
| Command | ใช้ทำอะไร |
|---|---|
npm cache verify | ตรวจ npm cache |
npm cache clean --force | ล้าง cache |
npm config list | ดู config |
npm config get registry | ดู registry ปัจจุบัน |
npm doctor | diagnose environment |
Publish
| Command | ใช้ทำอะไร |
|---|---|
npm login | login registry |
npm whoami | เช็กบัญชี |
npm version patch | bump version |
npm publish --access public | publish package |
npm deprecate <pkg>@<ver> "msg" | เตือนเวอร์ชันเก่า |