TypeError: Reduce of Empty Array with No Initial Value (Fix Explained)

While working with JavaScript arrays, you might have seen this error:

TypeError: Reduce of empty array with no initial value

This error is very common, especially when using the reduce() method.

In this post, we’ll understand:

  • What this error means
  • Why it happens
  • How to fix it properly
  • Best practices to avoid it

All explained in a simple and clear way.


What is reduce()?

Before understanding the error, let’s quickly recap:

reduce() is used to:

Convert an array into a single value (sum, object, etc.)

Example:

const numbers = [10, 20, 30];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // 60

The Error

Now look at this:

[].reduce((acc, curr) => acc + curr);

👉 Output:

TypeError: Reduce of empty array with no initial value

Why Does This Error Occur?

To understand this, you must know how reduce() works internally.


Case 1: Without Initial Value

[10, 20, 30].reduce((acc, curr) => acc + curr);

Internally:

  • acc = 10 (first element)
  • curr = 20 (second element)

Then continues…


Problem with Empty Array

Now consider:

[].reduce((acc, curr) => acc + curr);

There is:

  • ❌ No first element
  • ❌ No second element
  • ❌ Nothing to assign to acc

👉 JavaScript gets confused and throws an error.


Root Cause

reduce() needs an initial value or at least one element to start with.

If both are missing → 💥 Error


Solution 1: Always Provide Initial Value

[].reduce((acc, curr) => acc + curr, 0);

👉 Output:

0

Why this works:

  • acc = 0 (initial value)
  • No elements → loop doesn’t run
  • Returns 0

Solution 2: Check Array Before Reduce

function sumArray(arr) {
if (arr.length === 0) return 0;
return arr.reduce((acc, curr) => acc + curr);
}

Solution 3: Safe Reduce Pattern

const sum = (arr || []).reduce((acc, curr) => acc + curr, 0);

👉 Handles:

  • null
  • undefined
  • empty array

Real-World Scenario

Imagine:

const cartItems = [];

You calculate total:

const total = cartItems.reduce((sum, item) => sum + item.price);

💥 App crashes if cart is empty!


Correct Approach

const total = cartItems.reduce((sum, item) => sum + item.price, 0);

Common Mistake Developers Make

❌ Forgetting initial value:

arr.reduce((acc, curr) => acc + curr);

✅ Correct:

arr.reduce((acc, curr) => acc + curr, 0);

Best Practice

Always provide an initial value when using reduce()


Simple Analogy

Imagine you are adding numbers:

  • If someone gives you numbers → you can start
  • If no numbers are given → what will you add? 🤔

👉 You need a starting value (like 0)


With vs Without Initial Value

CaseBehavior
With initial valueSafe, works always
Without initial valueFails for empty array

Interview Tip

If asked:

Why does this error occur?

Answer:

The error occurs because reduce() tries to use the first element as the accumulator, but in an empty array, no such element exists. Providing an initial value solves this issue.


🏁 Final Summary

  • reduce() combines array elements into one value
  • Without initial value:
    • Uses first element as accumulator
    • Fails for empty arrays
  • With initial value:
    • Safe and predictable
    • Works for all cases

👉 Always use an initial value to avoid this error

💡 Found this helpful? Subscribe to get real-world coding tips, interview questions, and easy explanations directly in your inbox. Happy Coding!

Solution : husky – pre-commit hook exited with code 127 (error)

While working with Git and committing code, you might encounter this error:

husky - pre-commit hook exited with code 127 (error)

This error is very common when using Husky for Git hooks.

In this post, we’ll understand:

  • What this error means
  • Why it happens
  • How to fix it step-by-step
  • Temporary and permanent solutions

What is Husky?

Husky is a tool that helps you run scripts before Git actions like:

  • commit
  • push

For example:

  • Running ESLint before commit
  • Preventing bad code from being committed

The Error

husky - pre-commit hook exited with code 127 (error)

What Does Error Code 127 Mean?

Error code 127 means: “Command not found”

👉 In simple terms:
Husky is trying to run a command, but that command does not exist or cannot be found.


Why This Error Happens

This error usually occurs due to one of the following reasons:

1️⃣ Missing Dependencies

The command inside your pre-commit hook (like eslint, lint-staged) is not installed.


2️⃣ Incorrect Path

The script is not able to locate the command.


3️⃣ Node Modules Not Installed

You forgot to run:

npm install

4️⃣ Husky Not Installed Properly

Husky setup might be broken or incomplete.


5️⃣ Shell Environment Issues

Sometimes Git hooks don’t have access to your system PATH.


Quick Temporary Fix

If you want to skip the error and commit anyway:

git commit -m "your message" --no-verify

👉 This bypasses Husky hooks.

⚠️ Use this only as a temporary fix.

Above solution will also work for the following error too.

husky – pre-commit script failed(code 127)


Permanent Fix (Step-by-Step)


🔹 Step 1: Install Dependencies

npm install

🔹 Step 2: Check Husky Installation

npx husky install

🔹 Step 3: Verify Pre-Commit Hook

Open:

.husky/pre-commit

Make sure it contains valid commands like:

npx lint-staged

🔹 Step 4: Install Missing Tools

If you are using lint-staged:

npm install lint-staged --save-dev

🔹 Step 5: Use npx Instead of Direct Commands

❌ Wrong:

eslint .

✅ Correct:

npx eslint .

👉 This ensures the command runs from local dependencies.


🔹 Step 6: Reinstall Husky (If Needed)

rm -rf node_modules
rm -rf .huskynpm install
npx husky install

Real-World Scenario

You clone a project and try to commit:

git commit -m "initial commit"

💥 Error appears because:

  • Dependencies are not installed

👉 Fix:

npm install

Common Mistakes

❌ Not running npm install
❌ Using global packages instead of local
❌ Incorrect Husky setup
❌ Missing lint-staged


Best Practice

  • Always install dependencies before working
  • Use npx for commands
  • Keep Husky properly configured

Final Summary

  • Error 127 = Command not found
  • Happens due to missing dependencies or wrong setup
  • Use --no-verify for temporary fix
  • Fix permanently by installing and configuring tools properly

💡 Facing more errors like this? Subscribe to get simple fixes, real-world debugging tips, and developer-friendly explanations. Happy Coding!

Related Error Fixes You May Find Helpful: