2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Astro.js と astro-compress をアップデートしたら Sharp でエラーが出た

Last updated at Posted at 2023-12-28

Astro.js と astro-compress をアップデートした

本日(2023年12月28日)、管理下のサイトにてnodeモジュールのアップデートを行ったところ、開発機のWindowsでは問題なかったのだが、ビルドを行うLinux上でエラーが発生した。

以下がアップデート内容(npm-check-updatesを利用)

@astrojs/check     ^0.3.1  →   ^0.3.4
@astrojs/sitemap   ^3.0.3  →   ^3.0.4
astro              ^4.0.3  →   ^4.0.8
astro-compress     ^2.2.3  →   ^2.2.5

エラーメッセージ

$npm run build

[vite] Error when evaluating SSR module /path_to_project/astro.config.mjs: failed to import "astro-compress"
|- Error: Could not load the "sharp" module using the linux-x64 runtime
ERR_DLOPEN_FAILED: libvips-cpp.so.42: cannot open shared object file: No such file or directory
Possible solutions:
- Add platform-specific dependencies:
    npm install --os=linux --cpu=x64 sharp
  or
    npm install --force @img/sharp-linux-x64
- Consult the installation documentation: https://sharp.pixelplumbing.com/install
    at Object.<anonymous> (/path_to_project/node_modules/astro-compress/node_modules/sharp/lib/sharp.js:85: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> (/path_to_project/node_modules/astro-compress/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)

[astro] Unable to load your Astro config

Could not load the "sharp" module using the linux-x64 runtime
ERR_DLOPEN_FAILED: libvips-cpp.so.42: cannot open shared object file: No such file or directory
Possible solutions:
- Add platform-specific dependencies:
    npm install --os=linux --cpu=x64 sharp
  or
    npm install --force @img/sharp-linux-x64

いや、もちろんSharpの記事を書いているくらいなので、Sharpのクロスプラットフォーム問題があるのは知っているが・・・
何故いままで問題なかったのに出てきたのか?

【NG】とりあえずSharpをインストールしてみる

エラーメッセージにある通りにSharpをインストールしてみる

npm install --os=linux --cpu=x64 sharp
or
npm install --force @img/sharp-linux-x64

が、どちらも駄目。
そりゃ、AstroやAstro-compressに含まれているのでインストールしたところで使われないし。

package-lock.jsonnode_module を一旦削除してみても駄目。

【原因判明】Astro と Astro-compress の Sharp の利用バージョンが違う

よくよく見てみるとAstroとAstro-compressの Sharp の利用バージョンが異なっている。

Package Version Sharp Version
astro 4.0.8 0.33.1 (以上)
astro-compress 2.2.5 0.33.0 (固定)

なるほどこれか。
更に深掘りすると、astro-compress側がバージョンを固定してしまっている 模様。

解決策

1. astro-compressのアップデートを待つ(推奨)

いまは何も触らず、恐らく次のアップデートで対応されると思わるのでそれまで待つ。

2. astro と astro-compress が共存できる最新バージョンまで下げる(推奨)

AstroのChangeLogを見ると、4.0.8でSharpのアップデートを行っているので、4.0.7に下げれば良い

[訂正]
astro-compress側が2.2.5でバージョンを固定しているので、 astro-compressを2.2.3(2.2.4は存在しない)にすればエラーは消える。

その上で astro-compress が追いついたら再度アップデートをする。

3. Sharpのバージョンを固定する

package.json に overrides(npm)resolutions(yarn) を追記してSharpのバージョンを固定してしまう。

# npmの場合
"overrides": {
  "sharp": "0.33.1"
}

# yarnの場合
"resolutions": {
  "astro/sharp": "0.33.1",
  "astro-compress/sharp": "0.33.1"
},

これを行った場合、Sharpのアップデートがあったときに解除するかバージョンを自分で書き直す必要が発生するため、かなり面倒くさい。

まとめ

Astroとastro-compress、Sharpのアップデートのタイミングにより今後もぼちぼち出そうなので忘備録として書いてみた。
Sharpは便利だけど面倒くさい。

では、皆さん良いお年を。

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?