Uncaught TypeError: (init_os(...) , os_exports).platform is not a function (Vite & PNPM)
Are you using the latest version? Is the version currently in use as reported by npm ls sharp the same as the latest version as reported by npm view sharp dist-tags.latest?
0.29.3
What are the steps to reproduce?
Trying to use the library with Vite and PNPM, but I also tried with NPM & Yarn and it didn't make a difference. @types/sharp installed.
What is the expected behaviour?
Starting my application without the Uncaught TypeError.
Are you able to provide a minimal, standalone code sample, without other dependencies, that demonstrates this problem?
import sharp from 'sharp'
export const processImages = (photos: string[]): FormData => {
const formData = new FormData()
photos.forEach((photo) => {
const base64string = photo.replace('data:image/jpeg;base64,', '')
const buff = Buffer.from(base64string, 'base64')
const processed = sharp(buff)
.jpeg({ quality: 60 })
.toFile('filename.jpg', (err, info) => {
console.log(info)
// formData.append("filename", processed, "filename.jpg")
})
})
return formData
}
Standalone code should be fine, I can only imagine it must have to do something with Vite.
Are you able to provide a sample image that helps explain the problem?

What is the output of running npx envinfo --binaries --system?
System:
OS: Windows 10 10.0.19043
CPU: (16) x64 AMD Ryzen 9 5900HX with Radeon Graphics
Memory: 3.53 GB / 13.87 GB
Binaries:
Node: 17.3.1 - C:\Program Files\nodejs\node.EXE
Yarn: 1.23.0-20210726.1745 - ~\AppData\Roaming\npm\yarn.CMD
npm: 8.3.1 - C:\Program Files\nodejs\npm.CMD
I think I might have come closer to the solution. While reading the installation instructions again, I noticed the section about externals in webpack. Now the equivalent in Vite seems to be
vite.config.ts
export default defineConfig({
...
optimizeDeps: {
include: ['vue', 'vue-router'],
exclude: ['sharp']
}
})
Now the only problem I am left with is:
Uncaught SyntaxError: The requested module '/node_modules/.pnpm/[email protected]/node_modules/sharp/lib/index.js?v=6401a4b2' does not provide an export named 'default'
This module requires a Node.js runtime. It looks like you're trying to run it in a web browser, which won't work.
@lovell Oh, so it only is usable in a node backend, not in a frontend? 😮
That's correct, it's a Node.js module.
Oh, my bad. I thought I could simply use the library in a Vue frontend to send optimized images to my backend which is written in Go. 😕 Thanks for clarifying.