'Unable to import 'sharp' into next.js project. "Module not found: Can't resolve 'child_process'"

I am trying to import and use sharp in my Next.js project. I have tried uninstalling and reinstalling sharp many times but every time I run npm run dev, this is the error I get.

./node_modules/detect-libc/lib/detect-libc.js:3:0
Module not found: Can't resolve 'child_process'

Import trace for requested module:
./node_modules/sharp/lib/utility.js
./node_modules/sharp/lib/index.js
./pages/index.js

https://nextjs.org/docs/messages/module-not-found

For reference this is the code that is causing the error:

import sharp from "sharp";

I have also tried this code, with no success:

const sharp = require('sharp');

Every other dependency I have installed works except for sharp.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source