Last modified: Jun 24, 2026

Fix Cannot Find Module Winston

Running a Node.js application and seeing the error "Cannot find module 'winston'" can be frustrating. This error usually means your project cannot locate the winston logging library. It is a common issue for beginners and experienced developers alike. This guide will help you fix it quickly and understand why it happens.

What Causes the Error?

The error occurs when Node.js tries to load the winston module but cannot find it in your node_modules folder. This can happen for several reasons. You may have forgotten to install it. You might be in the wrong directory. Or your package.json file could be missing the dependency. Let's explore each cause.

Solution 1: Install winston Properly

The most common fix is to install winston using npm. Open your terminal and run the following command inside your project folder.


npm install winston

This command downloads the library and adds it to your node_modules folder. It also updates your package.json file with the dependency. After installation, try running your app again.

If you are using yarn, use this command instead.


yarn add winston

Always check that the installation completed without errors. A successful install shows a progress bar and a final message.

Solution 2: Check Your Working Directory

Sometimes you are in the wrong folder. If you run your script from a different directory, Node.js will not find the module. Make sure your terminal is pointing to the root of your project where package.json lives. Use pwd on macOS/Linux or cd on Windows to verify.


pwd
# Output: /home/user/my-project

If you are not in the right place, navigate to your project folder first. Then run your script again.

Solution 3: Delete node_modules and Reinstall

Your node_modules folder may be corrupted or incomplete. Deleting it and reinstalling all dependencies can fix this. Run these commands in your project root.


rm -rf node_modules
npm install

This removes all installed packages and reinstalls everything from your package.json. It is a clean way to resolve many module errors, including the winston issue. After this, test your application.

Solution 4: Check for Global vs Local Installation

You might have installed winston globally instead of locally. Global modules are not accessible by default in your project. To check, run this command.


npm list -g winston

If it shows a global version, you need to install it locally as shown in Solution 1. Alternatively, you can link the global module, but local installation is recommended for project-specific dependencies.

Solution 5: Verify package.json

Your package.json file must list winston under dependencies or devDependencies. Open the file and check. If it is missing, add it manually or run npm install winston --save to update it automatically. A correct entry looks like this.


{
  "dependencies": {
    "winston": "^3.8.2"
  }
}

If the entry is present but the module is missing, run npm install to sync your node_modules with the file.

Example Code with winston

Here is a simple example to test if winston works after fixing the error. Create a file called app.js with this code.


const winston = require('winston'); // Import winston

// Create a logger
const logger = winston.createLogger({
  level: 'info',
  transports: [
    new winston.transports.Console() // Log to console
  ]
});

logger.info('Hello, winston works!'); // Log a message

Run the file with Node.js.


node app.js

If winston is installed correctly, you should see output similar to this.


{"level":"info","message":"Hello, winston works!"}

This confirms the module is working. If you still see the error, revisit the solutions above.

Other Common Module Errors

If you encounter similar issues with other packages, check our guide on Fix Node.js Error: Cannot find module. It covers general troubleshooting steps for missing modules in Node.js. The same principles apply to winston and many other libraries.

Conclusion

The "Cannot find module 'winston'" error is easy to fix. Start by installing winston locally with npm install winston. Verify you are in the correct project directory. If needed, delete and reinstall your node_modules folder. Always check your package.json for the dependency entry. With these steps, your logging setup will work smoothly. Remember to test with a simple script to confirm success. Happy coding