تثبيت npm باستخدام fnm
npm (Node Package Manager) يتم تثبيته مع Node.js. يقدم هذا الدليل كيفية إدارة npm عبر fnm.
العلاقة بين npm و fnm
عند تثبيت Node.js باستخدام fnm، يتم تضمين npm تلقائياً:
bash
# Install Node.js (automatically includes npm)
fnm install 20
# Verify npm version
npm --versionالتحقق من إصدار npm
bash
# View current npm version
npm --version
# View Node.js and npm versions
node --version
npm --versionتتوافق إصدارات Node.js المختلفة مع إصدارات npm المختلفة:
| Node.js | npm |
|---|---|
| 20.x | 10.x |
| 18.x | 9.x |
| 16.x | 8.x |
ترقية npm
باستخدام الترقية الذاتية لـ npm
bash
# Upgrade to latest version
npm install -g npm@latest
# Upgrade to specific version
npm install -g npm@10.2.0تبديل إصدار Node.js
تبديل إصدار Node.js يبدل أيضاً إصدار npm:
bash
# Switch to Node.js 20
fnm use 20
npm --version # Output: 10.x.x
# Switch to Node.js 18
fnm use 18
npm --version # Output: 9.x.xإدارة الحزم العالمية لـ npm
عرض مسار التثبيت العالمي
bash
npm config get prefixموقع الحزم العالمية
توجد حزم Node.js العالمية التي يديرها fnm في:
~/.fnm/node-versions/<version>/installation/lib/node_modulesتثبيت الحزم العالمية
bash
# Install global packages
npm install -g pnpm
npm install -g yarn
npm install -g typescript
# View global packages
npm list -g --depth=0استخدام Corepack
Corepack هو مدير مدير الحزم لـ Node.js والذي يمكنه إدارة npm و yarn و pnpm:
تمكين Corepack
bash
# Enable Corepack when installing Node.js
fnm install 20 --corepack-enabled
# Or set environment variable
export FNM_COREPACK_ENABLED=true
fnm install 20استخدام Corepack
bash
# Enable Corepack
corepack enable
# Use specific version of yarn
corepack prepare yarn@stable --activate
# Use specific version of pnpm
corepack prepare pnpm@latest --activateتكوين npm
تكوين المرآة
bash
# Set npm mirror
npm config set registry https://registry.npmmirror.com
# View current configuration
npm config list
# Restore default mirror
npm config set registry https://registry.npmjs.orgالمرآات الشائعة
| مصدر المرآة | URL |
|---|---|
| npm الرسمي | https://registry.npmjs.org |
| npmmirror | https://registry.npmmirror.com |
| Tencent Cloud | https://mirrors.cloud.tencent.com/npm/ |
إدارة npm متعدد الإصدارات
بما أن npm يتم تثبيته مع Node.js، فإن أفضل طريقة لإدارة إصدارات npm المتعددة هي:
الطريقة 1: تبديل إصدار Node.js
bash
# Install multiple Node.js versions
fnm install 20
fnm install 18
# Switch versions
fnm use 20 # npm 10.x
fnm use 18 # npm 9.xالطريقة 2: ترقية npm بشكل مستقل
bash
# Upgrade npm in current Node.js version
npm install -g npm@latestالمشكلات الشائعة
أمر npm غير موجود
bash
# Confirm Node.js is installed
fnm list
# Confirm currently used version
fnm current
# Reinstall Node.js
fnm install 20 --useفشل تثبيت الحزمة العالمية
bash
# Clear npm cache
npm cache clean --force
# Check permissions
ls -la ~/.fnm/node-versions/
# Reinstall
npm install -g <package>عدم تطابق إصدار npm
bash
# Check Node.js version
fnm current
# Reset npm to default version
fnm uninstall <version>
fnm install <version>أفضل الممارسات
1. استخدام package.json engines
تحديد إصدارات Node.js و npm في المشروع:
json
{
"engines": {
"node": ">=18.0.0",
"npm": ">=9.0.0"
}
}2. استخدام ملف .npmrc
أنشئ ملف .npmrc في المشروع:
registry=https://registry.npmmirror.com
save-exact=true3. قفل مدير الحزم
استخدم حقل packageManager:
json
{
"packageManager": "npm@10.2.0"
}روابط ذات صلة
- تثبيت Node.js - تثبيت Node.js
- إدارة الإصدارات - دليل إدارة الإصدارات
- مرجع الأوامر - جميع تفاصيل الأوامر