الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

npm

Install the packages of a Node.js project, run its scripts (build, test) and check dependencies for known vulnerabilities.

المستوى
أداة DevOps
المجموعة
أدوات البناء ولغات البرمجة
يحتاج root
لا
الحزمة
nodejs

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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.
run SCRIPT
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.
version patch|minor|major
Raise the version and make a Git tag.
publish / pack
Upload the package / make a .tgz.
config get|set / login
Settings / log in to a registry.
cache clean --force / exec / npx
Clear the cache / run a package binary.

الخيارات 9

الأكثر استخدامًا

Options

--omit=dev
Do not install devDependencies (production image).
-D, --save-dev
Add as a devDependency.
-E, --save-exact
Save the exact version, not a range.
-g, --global
Install globally.
--registry URL
Use another registry (Nexus, Artifactory, GitHub Packages).
--audit-level=high
Fail npm audit only from this severity.
--ignore-scripts
Do not run install scripts of packages (safer).
-w NAME / --workspaces
Run in one / all workspaces of a monorepo.
--prefix DIR
Run in another folder.

أمثلة 12

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Install and test in CI

    npm ci
    npm test
  2. 2Build the production files

    npm run build
  3. 3Fail the pipeline on high and critical vulnerabilities

    npm audit --audit-level=high
  4. 4A 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"]
  5. 5Add a package with an exact version

    npm install express@4.21.2 --save-exact
  6. 6Add a dev tool

    npm install -D eslint
  7. 7What is old?

    npm outdated
  8. 8Release a patch version (tag + commit)

    npm version patch -m "release %s"
    git push --follow-tags
  9. 9Use a private registry

    npm config set registry https://nexus.example.com/repository/npm/
    npm ci
  10. 10Why is this package here?

    npm ls lodash
  11. 11Run a tool without installing it

    npx prettier --check .
  12. 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 install in CI can change the lock file; use npm ci.
  • Install scripts of packages run code on your machine; --ignore-scripts in CI is safer when the build allows it.
  • Never put a registry token in .npmrc in 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 ~/.npm makes CI installs fast.

رموز الخروج

0OK.
1Error (failed script, audit found problems, install failed).

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man npm