likes
comments
collection
share

Bug ReferenceError: AbortController is not defined

作者站长头像
站长
· 阅读数 23

背景

node server 提供 ipfs 文件上传服务

源码

const { create, globSource }  = require('ipfs-http-client');

this.ipfs = create({ host: host, port: port, protocal: protocal });

async addAll(files) {
    let addOptions = {
        pin: true,
        // wrapWithDirectory: true,
        timeout: 10000
    };
    try {
        let res = []
        for await (const result of this.ipfs.addAll(files, addOptions)) {
            res.push({
                path: result.path,
                size: result.size
            })
        }
        return res
    } catch (error) {
        throw error
    }
}

调用 addAll() 方法提示错误

ReferenceError: AbortController is not defined

原因

AbortController 对 node 版本有要求,要求 v16.xx.xx,所以只需要升级 node 版本即可。

升级 node

服务器系统是 centos,可以选择 node 版本管理工具 n

安装 n

npm install -g n

下载 node

n 10.16.0 // 指定版本下载
n lts // 最新版本下载

切换 node 版本

n
ο node/8.11.3
  node/10.15.0
  node/v16.8.0

查看当前版本

node -v

如果这里发现还是原来的版本,没有升级成功有可能是本地 node 指向问题,n 安装的源文件默认安装在 /usr/local/bin 下面,先检查下当前指向

which node

如果没有指向 /usr/local/bin,修改本地配置

vim ~/.bash_profile

N_PROFIX=/usr/local
PATH=$N_PROFIX/bin:$PATH

export PATH

查看当前版本

node -v
v16.8.0

结果

升级结束后,重新安装依赖启动服务,再次调用 addAll 方法,调用成功。

总结

从遇到问题到解决问题,花费时间主要在升级 node 版本而不是定位问题。遇到这个问题不能说明什么,毕竟问题实在太多纯靠积累,但是在 node 生态工具的使用上还是非常欠缺花费了不少时间,后续可以在这方面深入一点。