前端依赖安装错误识别不到.node文件引入

报错详情

macOS系统,M1 Pro芯片,node版本20.9.0,vue版本3,项目中引入的node依赖文件是fsevents

项目启动过程中显示报错

[ERROR] No loader is configured for ".node" files: node_modules/fsevents/fsevents.node

解决办法

修改项目中./node_modules/fsevents/fsevents.js 文件

//修改
const Native = require("./fsevents.node")
//更改为
const Native = window.require("./fsevents.node")

举一反三

对于这种「.node」文件的引入报错,基本都可以这样修改,将「require」改为「window.require」即可正常引入

后期补充

该方法并不是万能的,我又找到了一种方法,当上面添加「window」的方法不能用的时候,可以在「vite.config.js」中添加如下配置:

// vite.config.js
optimizeDeps: {
    exclude: ["fsevents"],
  },

本作品采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 (CC BY-NC-ND 4.0) 进行许可。