Last modified: Jun 28, 2026

Fix Cannot find module 'sinon'

Getting the error Cannot find module 'sinon' is a common issue for Node.js developers. This error stops your test suite from running. It usually happens when Sinon.js is missing from your project dependencies.

Don't worry. This guide will help you fix it quickly. We will cover the main causes and simple solutions. You will learn how to prevent this error in the future.

What is Sinon.js?

Sinon.js is a popular testing library for JavaScript. It provides test doubles like spies, stubs, and mocks. Developers use it to isolate code during unit testing. It works well with test frameworks like Mocha and Jest.

When you try to import or require Sinon, Node.js looks for it in the node_modules folder. If it's not there, you see the Cannot find module 'sinon' error.

Common Causes of the Error

Here are the most common reasons why this error appears:

  • Sinon is not installed in your project.
  • You are in the wrong directory.
  • The node_modules folder is corrupted.
  • Your package.json does not list Sinon.
  • You are using an outdated version of Node.js or npm.

Understanding the cause helps you choose the right fix. Let's go through each solution step by step.

Solution 1: Install Sinon Locally

The most straightforward fix is to install Sinon as a dev dependency. Run this command in your project root:


npm install --save-dev sinon

This command adds Sinon to your package.json under devDependencies. It also downloads the package into the node_modules folder.

After installation, try running your tests again. The error should be gone.

Solution 2: Check Your Working Directory

Make sure you are in the correct project folder. If you are outside your project, Node.js cannot find the module. Use pwd (Linux/Mac) or cd (Windows) to verify your location.

For example, if your project is in /home/user/my-project, navigate there first:


cd /home/user/my-project
npm install --save-dev sinon

Then run your test file again.

Solution 3: Reinstall All Dependencies

Sometimes the node_modules folder gets corrupted. Deleting it and reinstalling everything often fixes the issue. Follow these steps:


# Delete node_modules and package-lock.json
rm -rf node_modules package-lock.json

# Reinstall all dependencies
npm install

This ensures a clean state. After the reinstall, check if Sinon is now available.

Solution 4: Install Sinon Globally

If you use Sinon in multiple projects, you can install it globally. However, this is not recommended for production. Global installs can cause version conflicts.

To install globally, run:


npm install -g sinon

Then in your test file, you can require it directly. But for better practice, always use local installations.

Solution 5: Clear npm Cache

A corrupted npm cache can also cause module errors. Clear the cache and reinstall Sinon:


npm cache clean --force
npm install --save-dev sinon

This removes any cached data that might be broken. Then you get a fresh copy of Sinon.

Solution 6: Use the Correct Import Syntax

Make sure you are using the right syntax to import Sinon. In Node.js with CommonJS, use require:


// Correct CommonJS syntax
const sinon = require('sinon');

If you are using ES modules, use import:


// Correct ES module syntax
import sinon from 'sinon';

Using the wrong syntax for your project type can also trigger the error.

Example Code with Sinon

Here is a simple test example using Sinon with Mocha. It shows how to create a stub.


// math.js - simple function
function add(a, b) {
    return a + b;
}

module.exports = { add };

// test.js - using Sinon stub
const sinon = require('sinon');
const { add } = require('./math');
const assert = require('assert');

describe('Math functions', function() {
    it('should call add and return 5', function() {
        // Create a stub for the add function
        const stub = sinon.stub().returns(5);
        
        // Call the stub
        const result = stub(2, 3);
        
        // Verify the result
        assert.strictEqual(result, 5);
        console.log('Test passed: Stub returned 5');
    });
});

Run the test with Mocha:


npx mocha test.js

Expected output:


  Math functions
    ✓ should call add and return 5

  1 passing (5ms)

This code works only if Sinon is installed correctly. If you get the Cannot find module 'sinon' error, go back to the solutions above.

Preventing the Error in the Future

To avoid this error, always install Sinon as a dev dependency when you start a new project. Add it to your package.json before writing tests. Also, commit your package.json and package-lock.json to version control. This ensures other developers get the same dependencies.

If you clone a repository, always run npm install first. This installs all dependencies listed in package.json, including Sinon.

For more help with module errors, check out our guide on how to fix Node.js module errors.

Conclusion

The Cannot find module 'sinon' error is easy to fix. The main solution is to install Sinon locally with npm install --save-dev sinon. If that fails, check your directory, reinstall dependencies, or clear the npm cache.

Always use the correct import syntax for your project. By following these steps, you can get back to writing and running your tests quickly.

Remember to keep your dependencies updated and node_modules clean. This prevents many common module errors in Node.js.