Last modified: Jun 22, 2026
Fix Sharp Module Not Found Error
The "Cannot find module 'sharp'" error is common in Node.js projects. It stops your app from running. This guide shows you how to fix it quickly. We will cover causes, solutions, and code examples.
Sharp is a popular library for image processing. It resizes, converts, and optimizes images. When Node.js cannot find it, your app crashes. Let's solve this step by step.
Why does this error happen?
The error appears when Node.js cannot locate the sharp module. This usually means the module is not installed. It can also happen due to broken dependencies or platform issues. Another common cause is missing native binaries.
Sharp relies on native C++ libraries. These binaries must match your operating system. If they are missing or corrupted, the error occurs. Let's look at the main reasons:
- Sharp is not installed in your project.
- Node.js version is incompatible with sharp.
- Native binaries failed to download during installation.
- You are using a different environment (e.g., Docker, CI/CD) without proper setup.
How to fix the error
Here are the most effective solutions. Try them in order. The first solution usually works for most users.
1. Install sharp correctly
Run this command in your project root:
npm install sharp
Or if you use Yarn:
yarn add sharp
This installs sharp and its native binaries. Wait for the installation to finish. Do not interrupt it. The binaries download during the install step. If you see network errors, try again.
2. Clear npm cache and reinstall
Sometimes the cache causes issues. Clear it first:
npm cache clean --force
Then delete the node_modules folder and package-lock.json file:
rm -rf node_modules package-lock.json
Finally, reinstall all dependencies:
npm install
This ensures a fresh install of sharp and all other modules. It often resolves corrupted files.
3. Rebuild sharp for your platform
If sharp installed but still fails, rebuild its native binaries:
npm rebuild sharp
Or use the sharp-specific command:
npx sharp-cli --install
This forces sharp to compile binaries for your current OS and Node.js version. It is useful after updating Node.js or moving projects between machines.
4. Check Node.js version compatibility
Sharp requires a specific Node.js version range. Check your Node version:
node -v
Then verify the sharp documentation for supported versions. For sharp v0.33, Node.js 18 or later is needed. If you use an older Node, upgrade it.
# Install Node Version Manager (nvm) first, then:
nvm install 20
nvm use 20
After changing Node version, reinstall sharp as shown in step 1.
5. Use a specific sharp version
Sometimes the latest sharp version has issues. Install a stable older version:
npm install [email protected]
This avoids bugs in newer releases. Check your project's requirements. If other packages depend on sharp, ensure version compatibility.
Example code and output
Let's see a simple example. Create a file test-sharp.js:
// test-sharp.js - Example using sharp
const sharp = require('sharp'); // import sharp module
async function processImage() {
try {
// Resize an image to 200x200 pixels
await sharp('input.jpg')
.resize(200, 200)
.toFile('output.jpg');
console.log('Image resized successfully');
} catch (error) {
console.error('Error processing image:', error.message);
}
}
processImage();
Run the script:
node test-sharp.js
If sharp is installed correctly, you see this output:
Image resized successfully
If the error persists, you see:
Error: Cannot find module 'sharp'
This confirms the module is missing. Follow the solutions above.
Additional troubleshooting tips
If none of the above works, try these advanced steps:
- Check for platform-specific issues: On Windows, install Visual Studio Build Tools. On Linux, install
libvipsdependencies:sudo apt-get install libvips-dev. - Use Docker: If you deploy with Docker, ensure your Dockerfile installs sharp correctly. Add
RUN npm install sharpafter copying package.json. - Try a different module: If sharp continues to fail, consider alternatives like
jimporcanvas. But sharp is fastest for production.
You might also encounter this error when running tests. Make sure to install devDependencies if sharp is listed there. Run npm install --dev to include them.
For a broader perspective on module errors, see our guide on Fix Node.js Error: Cannot find module. It covers general troubleshooting steps for missing modules.
Preventing the error in the future
Follow these best practices to avoid the sharp error:
- Always run
npm installafter cloning a project. - Use a
.nvmrcfile to lock Node.js version. - Add sharp to your
package.jsondependencies explicitly. - Test your app in a clean environment before deployment.
If you use CI/CD pipelines, cache the node_modules folder. But rebuild sharp if the OS changes. Many CI tools offer caching for npm packages.
Remember that sharp's native binaries are platform-specific. Moving a project from Mac to Linux requires reinstallation. Always run npm install on the target machine.
Conclusion
The "Cannot find module 'sharp'" error is solvable. Start by installing sharp with npm install sharp. If that fails, clear the cache and reinstall. Rebuild native binaries if needed. Check your Node.js version and try a specific sharp version. For persistent issues, verify platform dependencies.
By following these steps, you can get sharp working in minutes. Your image processing tasks will run smoothly. For more Node.js error solutions, explore our Fix Node.js Error: Cannot find module article. It covers similar errors for other packages.
Test your setup with the example code above. If you still face problems, check the sharp GitHub issues page. The community is active and helpful. Happy coding!