Skip to main content

[#4 Troubleshoot] npm ERR! The npm ci command can only install with an existing package-lock.json

Saya menemukan Error

+ npm ci
npm ERR! code EUSAGE
npm ERR! 
npm ERR! The npm ci command can only install with an existing package-lock.json or
npm ERR! npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or
npm ERR! later to generate a package-lock.json file, then try again.
npm ERR! 
npm ERR! Clean install a project
npm ERR! 
npm ERR! Usage:
npm ERR! npm ci
npm ERR! 
npm ERR! Options:
npm ERR! [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
npm ERR! [--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
npm ERR! [--strict-peer-deps] [--no-package-lock] [--foreground-scripts]
npm ERR! [--ignore-scripts] [--no-audit] [--no-bin-links] [--no-fund] [--dry-run]
npm ERR! [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
npm ERR! [-ws|--workspaces] [--include-workspace-root] [--install-links]
npm ERR! 
npm ERR! aliases: clean-install, ic, install-clean, isntall-clean
npm ERR! 
npm ERR! Run "npm help ci" for more info

✅ Error tersebut cukup jelas dan mudah diatasi.


❌ PENYEBAB ERROR:

Perintah npm ci membutuhkan file package-lock.json, tetapi file itu tidak ada di repo kamu saat Jenkins menjalankan pipeline.

Log error-nya bilang:

The `npm ci` command can only install with an existing package-lock.json

✅ SOLUSI:

✏️ Solusi #1 (Direkomendasikan):

Tambahkan file package-lock.json ke dalam repo GitLab.

Langkahnya:

  1. Di lokal, jalankan:

    npm install
    

    Ini akan otomatis menghasilkan file package-lock.json

  2. Commit file package-lock.json ke GitLab:

    git add package-lock.json
    git commit -m "add lockfile"
    git push origin main
    
  3. Jalankan ulang build Jenkins kamu.

📌 package-lock.json wajib untuk npm ci, karena npm ci melakukan clean install berdasarkan isi lockfile tersebut — cocok untuk CI/CD dan memastikan semua dependency konsisten.


🛠 Solusi #2 (Alternatif jika tidak pakai lockfile):

Jika kamu tidak ingin pakai package-lock.json, kamu bisa ubah npm ci menjadi npm install di Jenkinsfile:

stage('Install dependencies') {
  steps {
    sh 'npm install'
    sh 'npx playwright install --with-deps'
  }
}

Namun ❗ ini tidak direkomendasikan untuk CI karena:

  • Dependency bisa berubah sewaktu-waktu

  • Bisa menyebabkan build tidak konsisten antar waktu/machine


✅ Kesimpulan:

Opsi Apa yang dilakukan
✅ Tambah package-lock.json Ideal untuk CI, fix dependency version
⚠️ Pakai npm install saja Lebih fleksibel, tapi tidak deterministik