vite打包问题

作者:Administrator 发布时间: 2024-10-10 阅读量:5 评论数:0

vite打包问题

问题

npm run build

> luxiry-search@0.0.0 build /usr/local/src/search/luxirty-search
> vite build

/usr/local/src/search/luxirty-search/node_modules/vite/bin/vite.js:2
import { performance } from 'node:perf_hooks'
       ^

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! luxiry-search@0.0.0 build: `vite build`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the luxiry-search@0.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2024-10-10T13_24_54_755Z-debug.log

原因

这个错误通常是由于 Node.js 版本过旧,无法支持 import 语法。要解决这个问题,请尝试以下步骤:

  1. 检查 Node.js 版本

    • 运行 node -v 查看当前的 Node.js 版本。
    • Vite 通常需要 Node.js 14 或更高版本。
  2. 升级 Node.js

    • 如果你的版本低于 14,建议升级 Node.js。
  3. 使用 Node Version Manager (NVM)

    • 安装 NVM(如果还没有安装):
      curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
      source ~/.bashrc
      
    • 使用 NVM 安装最新的 Node.js 版本:
      nvm install node
      nvm use node
      
  4. 重试构建

    • 升级 Node.js 后,重新运行 npm run build

如果问题依然存在,请检查 vite 和其他依赖项是否正确安装,并确认 package.json 中的配置没有问题。

验证

从原来ubuntu默认使用apt安装的node,换成新的node22,果然可以了

重新安装一下依赖,可以正常打包了~

root@halo:/usr/local/src/search/luxirty-search# npm add vite

added 33 packages in 13s

5 packages are looking for funding
  run `npm fund` for details
root@halo:/usr/local/src/search/luxirty-search# npm run build

> luxiry-search@0.0.0 build
> vite build

vite v5.4.8 building for production...
✓ 31 modules transformed.
dist/index.html                  0.79 kB │ gzip:  0.54 kB
dist/assets/index-nybaKXs3.css  10.63 kB │ gzip:  2.79 kB
dist/assets/index-DvJpZOwv.js   85.68 kB │ gzip: 33.86 kB
✓ built in 1.59s

评论