Vue2老项目如何跑在最新`Node.js`上

最近手上有个老的vue2项目,不过项目比较老,定制控件也比较多,跑在Node.js 16上面,而且也暂时没有升级可能性,通常用nvm切换版本或者指定node版本跑,不少新项目升级了Node.js了,测试以及开发都统一使用新的Node.js了,因此以最简单的方式改造下老的项目,可以跑着Node.js24上面。

错误信息

Building for production...Error: error:0308010C:digital envelope routines::unsupported
at new Hash (node:internal/crypto/hash:68:19)
at Object.createHash (node:crypto:138:10)

这个错误通常出现在使用 Node.js 17 及以上版本时,由于 OpenSSL 3.x 默认安全策略导致某些加密算法被禁用,从而引发了 “error:0308010C:digital envelope routines::unsupported” 的错误。

编写脚本

build.js文件

const { spawn } = require('child_process')

const userArgs = process.argv.slice(2)
const action = userArgs.length > 0 ? userArgs.join(' ') : 'serve'
const baseCommand = `vue-cli-service ${action}`

const [major] = process.versions.node.split('.')
const nodeMajor = parseInt(major, 10)

const env = { ...process.env }
if (nodeMajor >= 17) {
  env.NODE_OPTIONS = (env.NODE_OPTIONS ? env.NODE_OPTIONS + ' ' : '') + '--openssl-legacy-provider'
}

console.log(`[build.js] 当前 Node 版本:${process.versions.node},正在执行:${baseCommand}`)

const child = spawn(baseCommand, {
  shell: true,
  stdio: 'inherit',
  env
})

child.on('exit', (code) => {
  process.exit(code || 0)
})

child.on('error', (err) => {
  console.error(`[build.js] 执行出错:${err.message}`)
  process.exit(1)
})

配置package.json

  "scripts": {
    "serve": "node build.js serve",
    "build": "node build.js build"
  }

配置完成,运行和构建还是使用原来的命令即可。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇