You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vite+ESBuild报错:未配置.node文件加载器(fsevents.node)

解决Vite中"No loader is configured for '.node' files: node_modules/fsevents/fsevents.node"报错

问题分析

你遇到的错误是因为Vite(基于Rollup)默认不支持处理.node格式的原生Node.js模块,而fsevents是仅在macOS系统生效的文件监听依赖——在Windows/Linux环境下它会被安装但不会实际运行,却触发了Vite的模块解析逻辑。另外你安装的node-loader是Webpack专属加载器,Vite无法直接兼容,所以即使安装了也无效。

解决方案

方案一:排除fsevents(推荐,无需额外插件)

直接告诉Vite跳过fsevents的预构建和打包,因为非mac环境下根本用不上它:

  1. 在项目根目录创建/修改vite.config.js(TypeScript项目则为vite.config.ts),添加以下配置:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig({
  plugins: [react()],
  optimizeDeps: {
    exclude: ['fsevents'] // 跳过fsevents的预构建流程
  },
  build: {
    rollupOptions: {
      external: ['fsevents'] // 构建时将fsevents标记为外部依赖,不打包进产物
    }
  }
})

方案二:用Rollup插件处理.node文件(仅当你确实需要fsevents时)

如果是在macOS环境下且必须使用fsevents,需要安装Rollup兼容的原生模块处理插件:

  1. 安装依赖:
npm install rollup-plugin-native-modules --save-dev
  1. 修改vite.config.js:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import nativeModules from 'rollup-plugin-native-modules'

export default defineConfig({
  plugins: [
    react(),
    nativeModules() // 让Rollup能解析.node格式文件
  ]
})

额外优化:移除不必要的resolutions配置

你在package.json里指定的resolutions强制锁定fsevents版本为1.2.9,这可能导致兼容性问题。可以尝试移除该配置后重新安装依赖:

rm package-lock.json
rm -rf node_modules
npm install

内容的提问来源于stack exchange,提问作者Shrikant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 01:52:25