Dependency on sharp not declared correctly for yarn’s PnP loader
jGleitz opened this issue · comments
Installing this plugin when using yarn results in this error:
failed to load config from [projectDir]/vite.config.js
error during build:
Error: vite-imagetools tried to access sharp, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.
Required package: sharp (via "sharp/package.json")
Required by: vite-imagetools@npm:6.2.5 (via /home/[user]/.yarn/berry/cache/vite-imagetools-npm-6.2.5-83cd4268b2-10c0.zip/node_modules/vite-imagetools/dist/index.js)
[stacktrace]
This only happens when using Yarn.
Reproduce
The script below reproduces the problem:
Reproducing Script
#!/bin/bash
mkdir dependency-reproduce
cd dependency-reproduce
cat << EOF > package.json
{
"name": "vite-imagetools-dependency-reproduce",
"packageManager": "yarn@4.0.2",
"type": "module",
"devDependencies": {
"vite": "5.0.2",
"vite-imagetools": "6.2.5"
},
"scripts": {
"build": "vite build"
}
}
EOF
cat << EOF > vite.config.js
import {defineConfig} from 'vite'
import {imagetools} from 'vite-imagetools'
export default defineConfig({
plugins: [imagetools()]
})
EOF
cat << EOF > index.html
<!DOCTYPE html>
<html>
<head>
<title>vite-imagetools-dependency-reproduce</title>
</head>
<body>
</body>
</html>
EOF
yarn
yarn build
Replacing yarn
and yarn build
with npm install
and npm run build
‘fixes’ the problem, since npm is more lenient when loading packages than yarn’s PnP resolver.
Workaround
Users can work around the issue by running yarn add --dev sharp
.
I'll consider a PR, but I won't be fixing this myself and would suggest switching to pnpm
Was this fixed by #679? Can you try 6.2.9?
No, the issue is still present with 6.2.9.
Seems to be a similar error.
env:
"engines": {
"node": ">=20.9.0",
"yarn": ">=1.22.18"
},
"devDependencies": {
"vite": "^5.0.0",
"vite-imagetools": "^6.2.9",
}
on run vite
:
error when starting dev server:
Error: Could not load the "sharp" module using the darwin-x64 runtime
Possible solutions:
- Ensure optional dependencies can be installed:
npm install --include=optional sharp
yarn add sharp --ignore-engines
- Ensure your package manager supports multi-platform installation:
See https://sharp.pixelplumbing.com/install#cross-platform
- Add platform-specific dependencies:
npm install --os=darwin --cpu=x64 sharp
- Consult the installation documentation:
See https://sharp.pixelplumbing.com/install
at Object.<anonymous> (/Users/dmitriychernukho/Documents/chill/datsteam/codev-front/node_modules/sharp/lib/sharp.js:114:9)
at Module._compile (node:internal/modules/cjs/loader:1241:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
at Module.load (node:internal/modules/cjs/loader:1091:32)
at Module._load (node:internal/modules/cjs/loader:938:12)
at Module.require (node:internal/modules/cjs/loader:1115:19)
at require (node:internal/modules/helpers:130:18)
at Object.<anonymous> (/Users/dmitriychernukho/Documents/chill/datsteam/codev-front/node_modules/sharp/lib/constructor.js:10:1)
at Module._compile (node:internal/modules/cjs/loader:1241:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
worked for me:
yarn add sharp -D --ignore-engines
sorry, it doesn't seem to be the same problem. problem with package sharp
@chojnicki When I initialized Yarn Modern the problem went away
OP is correct and vite-imagetools
needs to correct its dependency definition.
For anyone wishing to unblock themselves, add this to your .yarnrc.yml
file:
packageExtensions:
vite-imagetools@*:
peerDependencies:
sharp: "*"
peerDependenciesMeta:
sharp:
optional: true
I've sent a fix for this here: #730. Would anyone be able to test it before I merge it?