Last modified: Jun 24, 2026
Fix Cannot find module 'pino' Error
Encountering the error "Cannot find module 'pino'" can be frustrating, especially when you are new to Node.js. This error usually means your project cannot locate the pino logging library. In this article, we will walk you through the most common causes and solutions. You will learn how to install pino correctly, check your project setup, and avoid this issue in the future.
What is the 'pino' Module?
Pino is a fast, low-overhead logging library for Node.js. It is widely used for structured logging in production applications. The error "Cannot find module 'pino'" typically occurs when you try to import or require pino in your code, but the module is not installed in your project's node_modules folder.
This issue can arise from a simple missing installation, a corrupted node_modules directory, or running your script in the wrong folder. Let's break down the solutions step by step.
Common Causes of the Error
Before fixing the error, it helps to understand why it happens. Here are the three most common reasons:
- Missing installation: You did not run
npm install pinoin your project. - Wrong directory: Your terminal is not in the same folder as your
package.jsonfile. - Corrupted node_modules: The
node_modulesfolder is incomplete or damaged.
Now, let's fix each of these scenarios.
Solution 1: Install pino Locally
The most straightforward fix is to install pino as a project dependency. Open your terminal, navigate to your project's root directory (where package.json lives), and run:
npm install pino
This command downloads the pino package and adds it to your node_modules folder. It also updates your package.json file with a new dependency entry. After installation, try running your script again.
If you are using yarn instead of npm, use:
yarn add pino
Here is a simple example of how to use pino after installation:
// app.js
const pino = require('pino');
const logger = pino();
logger.info('Hello, world!');
logger.error('This is an error message');
Expected output:
{"level":30,"time":1690000000000,"pid":12345,"hostname":"your-host","msg":"Hello, world!"}
{"level":50,"time":1690000000000,"pid":12345,"hostname":"your-host","msg":"This is an error message"}
If you see the JSON logs, the error is resolved.
Solution 2: Check Your Working Directory
A common mistake is running your Node.js script from a different directory than where you installed pino. For example, if you installed pino in /project but run your script from /project/src, Node.js cannot find the module.
To verify your current directory, use:
pwd
Make sure you are in the project root. If you are not, change to the correct folder:
cd /path/to/your/project
Then run your script again. This simple step often fixes the Cannot find module issue.
Solution 3: Reinstall node_modules
Sometimes your node_modules folder gets corrupted due to incomplete downloads or manual edits. The best way to fix this is to delete the folder and reinstall all dependencies.
First, delete the node_modules folder and the package-lock.json file (or yarn.lock):
rm -rf node_modules package-lock.json
Then reinstall all dependencies:
npm install
This command reads your package.json and installs everything fresh, including pino. After that, test your script again.
Solution 4: Install pino Globally (Not Recommended)
While you can install pino globally using npm install -g pino, this is not a good practice for project dependencies. Global installations can cause version conflicts and make your application less portable. Stick with local installations for reliability.
If you must use a global install for quick testing, ensure your script references the global path, but this is beyond the scope of this beginner guide.
How to Avoid This Error in the Future
To prevent the "Cannot find module 'pino'" error from happening again, follow these best practices:
- Always run npm install first: When you clone a project from GitHub or start a new one, run
npm installto download all dependencies. - Use a package.json: Keep your dependencies listed in
package.jsonso you can easily reinstall them. - Commit package-lock.json: This file locks the exact versions of your dependencies, ensuring consistency across environments.
- Double-check your directory: Before running any Node.js script, verify you are in the project root.
If you encounter similar issues with other modules, check out our guide on how to Fix Node.js Error: Cannot find module. It covers general troubleshooting steps that apply to any missing module.
Using pino with ES Modules
If you are using ES modules (with "type": "module" in your package.json), you need to import pino differently. Instead of require, use the import syntax:
// app.mjs
import pino from 'pino';
const logger = pino();
logger.info('Using ES modules with pino!');
Make sure your file has the .mjs extension or that your package.json includes "type": "module". The installation step remains the same: npm install pino.
Conclusion
The "Cannot find module 'pino'" error is easy to fix once you know the root cause. By installing pino locally, checking your directory, or reinstalling your dependencies, you can get your logging working in minutes. Remember to always use a package.json file and install dependencies locally to avoid future headaches.
If you continue to face issues, review your project structure and ensure you are following the steps above. For more general help with missing modules, refer to our detailed guide on Fix Node.js Error: Cannot find module. Happy logging with pino!