Dockerfile执行yarn build构建Nuxt.js镜像遇加密错误求解决
Nuxt.js Docker构建时出现ERR_OSSL_EVP_UNSUPPORTED错误
执行命令docker build . -t <YOUR_DOCKER_HUB_USERNAME>/my-nuxt-project构建Nuxt.js项目的Docker镜像时,在RUN yarn build步骤出现如下错误:
Step 5/13 : RUN yarn build ---> Running in 4dd3684952ba yarn run v1.22.19 $ nuxt build ℹ Production build ℹ Bundling for server and client side ℹ Target: server ℹ Using components loader to optimize imports ℹ Discovered Components: .nuxt/components/readme.md ✔ Builder initialized ✔ Nuxt files generated ℹ Compiling Client node:internal/crypto/hash:71 this[kHandle] = new _Hash(algorithm, xofLen); ^ Error: error:0308010C:digital envelope routines::unsupported at new Hash (node:internal/crypto/hash:71:19) at Object.createHash (node:crypto:133:10) at module.exports (/app/node_modules/webpack/lib/util/createHash.js:135:53) at NormalModule._initBuildHash (/app/node_modules/webpack/lib/NormalModule.js:417:16) at handleParseError (/app/node_modules/webpack/lib/NormalModule.js:471:10) at /app/node_modules/webpack/lib/NormalModule.js:503:5 at /app/node_modules/webpack/lib/NormalModule.js:358:12 at /app/node_modules/loader-runner/lib/LoaderRunner.js:373:3 at iterateNormalLoaders (/app/node_modules/loader-runner/lib/LoaderRunner.js:214:10) at Array.<anonymous> (/app/node_modules/loader-runner/lib/LoaderRunner.js:205:4) at Storage.finished (/app/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16) at /app/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9 at /app/node_modules/graceful-fs/graceful-fs.js:123:16 at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3) { opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ], library: 'digital envelope routines', reason: 'unsupported', code: 'ERR_OSSL_EVP_UNSUPPORTED' } Node.js v18.12.0 error Command failed with exit code 1.
使用的Dockerfile如下:
FROM node:lts as builder WORKDIR /app COPY . . RUN yarn install \ --prefer-offline \ --frozen-lockfile \ --non-interactive \ --production=false RUN yarn build RUN rm -rf node_modules && \ NODE_ENV=production yarn install \ --prefer-offline \ --pure-lockfile \ --non-interactive \ --production=true FROM node:lts WORKDIR /app COPY --from=builder /app . ENV HOST 0.0.0.0 EXPOSE 3000 CMD [ "yarn", "start" ]
解决方法
该错误源于Node.js 18默认启用的加密算法与旧版本Webpack(Nuxt 2依赖的版本)不兼容,以下是几种可行的修复方式:
- 修改build命令,添加兼容参数
在Dockerfile的RUN yarn build步骤中注入环境变量,强制使用旧加密算法:
RUN NODE_OPTIONS="--openssl-legacy-provider" yarn build
- 降级Node.js版本
将Dockerfile中的node:lts替换为Node.js 16镜像(Node.js 16的加密算法规则与旧Webpack兼容):
FROM node:16 as builder # 后续构建步骤保持不变 FROM node:16 # 后续运行步骤保持不变
- 升级项目依赖
如果项目允许,升级Nuxt到最新稳定版(如Nuxt 3),或单独升级Webpack至支持Node.js 18加密算法的版本,从根源解决兼容性问题。
内容的提问来源于stack exchange,提问作者João Denari
相关产品推荐
相关产品推荐

