Mastodon 在较新 Linux 发行版本下会遇到前端预编译出错的问题。

本文大概记录一下,如何修复 Mastodon 前端预编译错误?

本文测试环境:

$ git describe --tags
v4.2.0-beta2

$ git rev-parse HEAD
facfec1ba36cee27f232ebff90b990933719235a

$ rbenv --version
rbenv 1.2.0

$ ruby --version
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]

$ bundle --version
Bundler version 2.4.13

$ node --version
v20.5.1

$ yarn --version
1.22.19

ERR_OSSL_EVP_UNSUPPORTED

如果 Node.js 版本大于 17,编译时便会遇到 ERR_OSSL_EVP_UNSUPPORTED 错误。

$ RAILS_ENV=production bundle exec rails yarn:install assets:precompile
yarn install v1.22.19
[1/5] Resolving packages...
success Already up-to-date.
Done in 0.47s.
Compiling...
Compilation failed:
node:internal/crypto/hash:69
this[kHandle] = new _Hash(algorithm, xofLen);
                ^

Error: error:0308010C:digital envelope routines::unsupported
    at new Hash (node:internal/crypto/hash:69:19)
    at Object.createHash (node:crypto:138:10)
    at module.exports (/srv/app/mastodon/mastodon/node_modules/webpack/lib/util/createHash.js:135:53)
    at ConcatenatedModule._createIdentifier (/srv/app/mastodon/mastodon/node_modules/webpack/lib/optimize/ConcatenatedModule.js:563:16)
    at new ConcatenatedModule (/srv/app/mastodon/mastodon/node_modules/webpack/lib/optimize/ConcatenatedModule.js:445:27)
    at /srv/app/mastodon/mastodon/node_modules/webpack/lib/optimize/ModuleConcatenationPlugin.js:250:26
    at SyncBailHook.eval [as call] (eval at create (/srv/app/mastodon/mastodon/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:16)
    at SyncBailHook.lazyCompileHook (/srv/app/mastodon/mastodon/node_modules/tapable/lib/Hook.js:154:20)
    at /srv/app/mastodon/mastodon/node_modules/webpack/lib/Compilation.js:1351:37
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/srv/app/mastodon/mastodon/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/srv/app/mastodon/mastodon/node_modules/tapable/lib/Hook.js:154:20)
    at Compilation.seal (/srv/app/mastodon/mastodon/node_modules/webpack/lib/Compilation.js:1342:27)
    at /srv/app/mastodon/mastodon/node_modules/webpack/lib/Compiler.js:675:18
    at /srv/app/mastodon/mastodon/node_modules/webpack/lib/Compilation.js:1261:4
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/srv/app/mastodon/mastodon/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
    at AsyncSeriesHook.lazyCompileHook (/srv/app/mastodon/mastodon/node_modules/tapable/lib/Hook.js:154:20)
    at Compilation.finish (/srv/app/mastodon/mastodon/node_modules/webpack/lib/Compilation.js:1253:28)
    at /srv/app/mastodon/mastodon/node_modules/webpack/lib/Compiler.js:672:17
    at eval (eval at create (/srv/app/mastodon/mastodon/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
    at /srv/app/mastodon/mastodon/node_modules/webpack/lib/Compilation.js:1185:12
    at /srv/app/mastodon/mastodon/node_modules/webpack/lib/Compilation.js:1097:9
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
library: 'digital envelope routines',
reason: 'unsupported',
code: 'ERR_OSSL_EVP_UNSUPPORTED'
}

解决办法参考这个 stackoverflow 回答

在运行 RAILS_ENV=production bundle exec rails yarn:install assets:precompile 命令前,先设置 NODE_OPTIONS=--openssl-legacy-provider 环境变量即可。

export NODE_OPTIONS=--openssl-legacy-provider

isModuleDeclaration has been deprecated, please migrate to isImportOrExportDeclaration

编译时,你可能会遇到类似如下面的 isModuleDeclaration has been deprecated, please migrate to isImportOrExportDeclaration 提示。

$ export NODE_OPTIONS=--openssl-legacy-provider
$ RAILS_ENV=production bundle exec rails yarn:install assets:precompile
yarn install v1.22.19
[1/5] Resolving packages...
success Already up-to-date.
Done in 0.50s.
I, [2023-08-31T17:34:06.971888 #40650]  INFO -- : Writing /srv/app/mastodon/mastodon/public/assets/doorkeeper/admin/application-a644908e7bab54fb749be0f59fb64a7480bbf9c4c2b79d4a65791cb7ab4d8730.css
I, [2023-08-31T17:34:06.972252 #40650]  INFO -- : Writing /srv/app/mastodon/mastodon/public/assets/doorkeeper/admin/application-a644908e7bab54fb749be0f59fb64a7480bbf9c4c2b79d4a65791cb7ab4d8730.css.gz
I, [2023-08-31T17:34:06.973952 #40650]  INFO -- : Writing /srv/app/mastodon/mastodon/public/assets/doorkeeper/application-c93dac2ad9d65e3393e0e2c958481e86ef7a5e5b0f6ce406842a7b99b25a4850.css
I, [2023-08-31T17:34:06.974013 #40650]  INFO -- : Writing /srv/app/mastodon/mastodon/public/assets/doorkeeper/application-c93dac2ad9d65e3393e0e2c958481e86ef7a5e5b0f6ce406842a7b99b25a4850.css.gz
I, [2023-08-31T17:34:06.975223 #40650]  INFO -- : Writing /srv/app/mastodon/mastodon/public/assets/pghero/favicon-db10337a56c45eb43c22ff5019546b520fa22c7281d4d385f235cbca67ed26bb.png
I, [2023-08-31T17:34:06.982815 #40650]  INFO -- : Writing /srv/app/mastodon/mastodon/public/assets/pghero/application-fc5c893e805df52338bef3bda5a5431f74f1401da68e4f0381ac9ecb7a801e1a.js
I, [2023-08-31T17:34:06.982956 #40650]  INFO -- : Writing /srv/app/mastodon/mastodon/public/assets/pghero/application-fc5c893e805df52338bef3bda5a5431f74f1401da68e4f0381ac9ecb7a801e1a.js.gz
I, [2023-08-31T17:34:06.984171 #40650]  INFO -- : Writing /srv/app/mastodon/mastodon/public/assets/pghero/application-29fa393e673685cdbf2fb2b637098a15b988605f46bfa962f6e0cb94d15bc567.css
I, [2023-08-31T17:34:06.984319 #40650]  INFO -- : Writing /srv/app/mastodon/mastodon/public/assets/pghero/application-29fa393e673685cdbf2fb2b637098a15b988605f46bfa962f6e0cb94d15bc567.css.gz
Compiling...
Compiled all packs in /srv/app/mastodon/mastodon/public/packs
`isModuleDeclaration` has been deprecated, please migrate to `isImportOrExportDeclaration`
    at isModuleDeclaration (/srv/app/mastodon/mastodon/node_modules/babel-plugin-lodash/node_modules/@babel/types/lib/validators/generated/index.js:2740:35)
    at PluginPass.Program (/srv/app/mastodon/mastodon/node_modules/babel-plugin-lodash/lib/index.js:102:44)

这只是编译警告,可以怱略不管。

如果一定要消除的话,可以参考: https://github.com/lodash/babel-plugin-lodash/issues/259#issuecomment-1438592335

清理编译失败产物

对于新版 Node.js 编译时会遇上 ERR_OSSL_EVP_UNSUPPORTED 错误,进而编译失败。

当你添加上环境变量,再次编译时,有可能会遇上 Everything's up-to-date. Nothing to do

$ RAILS_ENV=production bundle exec rails yarn:install assets:precompile
yarn install v1.22.19
[1/5] Resolving packages...
success Already up-to-date.
Done in 0.48s.
Everything's up-to-date. Nothing to do

这时可以运行如下命令,删去之前编译产物强制重新编译。

rm -r build public/system public/assets public/packs public/packs-test