هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
npm is the package manager of Node.js. package.json lists the dependencies and the scripts; package-lock.json pins the exact versions. In CI and in Dockerfiles you use npm ci: it installs exactly the lock file, fast and the same every time.
npm run NAME runs a script from package.json (for example build, test, lint).
طريقة الكتابة
npm COMMAND [ARGS] [OPTIONS]الأجزاء الرئيسية
Commands
ci- Clean install from package-lock.json (CI, Docker).
install/i[PKG]- Install all dependencies, or add a package.
runSCRIPT- Run a script from package.json.
test/start- Short for
npm run test/npm run start. audit[fix]- Check dependencies for known vulnerabilities.
outdated/update- Show / install newer versions in the allowed range.
ls[PKG]- Show the dependency tree.
versionpatch|minor|major- Raise the version and make a Git tag.
publish/pack- Upload the package / make a .tgz.
configget|set/login- Settings / log in to a registry.
cacheclean--force/exec/npx- Clear the cache / run a package binary.
الخيارات 9
الأكثر استخدامًا
Options
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 12
$ مستخدم عادي# بصلاحية root (أو مع sudo)
1Install and test in CI
npm ci npm test2Build the production files
npm run build3Fail the pipeline on high and critical vulnerabilities
npm audit --audit-level=high4A small production image (multi-stage)
cat Dockerfile FROM node:22-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM node:22-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --omit=dev --ignore-scripts COPY --from=build /app/dist ./dist USER node CMD ["node", "dist/server.js"]5Add a package with an exact version
npm install express@4.21.2 --save-exact6Add a dev tool
npm install -D eslint7What is old?
npm outdated8Release a patch version (tag + commit)
npm version patch -m "release %s" git push --follow-tags9Use a private registry
npm config set registry https://nexus.example.com/repository/npm/ npm ci10Why is this package here?
npm ls lodash11Run a tool without installing it
npx prettier --check .12Cache npm in GitHub Actions
cat .github/workflows/ci.yml - uses: actions/setup-node@v4 with: { node-version: 22, cache: npm } - run: npm ci && npm test
لا يوجد شيء يطابق هذا الفلتر.
انتبه
- Commit
package-lock.json. Without it every build can get other versions. npm installin CI can change the lock file; usenpm ci.- Install scripts of packages run code on your machine;
--ignore-scriptsin CI is safer when the build allows it. - Never put a registry token in
.npmrcin Git; use a CI secret.
الملفات
package.json- Name, version, scripts, dependencies.
package-lock.json- The exact versions of the whole tree.
.npmrc- Registry and auth settings (project or ~/.npmrc).
نصائح للحفظ
npm ci+ cache of~/.npmmakes CI installs fast.
رموز الخروج
0 | OK. |
1 | Error (failed script, audit found problems, install failed). |