reactjs - TypeError:fs.existsSync 不是函数
问题描述
我正在尝试Winston
在 React.js 中使用。
创建记录器时出现错误
TypeError:fs.existsSync 不是函数
这就是我创建它的方式:
this.logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
//
// - Write to all logs with level `info` and below to `combined.log`
// - Write all logs error (and below) to `error.log`.
//
new winston.transports.Console(),
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
我只看到有关如何在 Node.js 中处理它的问题。如何用 React 解决这个问题?
解决方案
fs 是一个 nodejs 库,所以你不能在浏览器端使用它。请改用winston-transport-browserconsole。
https://www.npmjs.com/package/winston-transport-browserconsole
它易于使用并支持记录 json 对象:
例子-
import * as winston from "winston";
import BrowserConsole from 'winston-transport-browserconsole';
const level = "debug";
winston.configure({
transports: [
new BrowserConsole(
{
format: winston.format.simple(),
level,
},
),
],
});
winston.debug("DEBUG ", {a: 1, b: "two"});
推荐阅读
- c# - Linux(ubuntu)单声道上的TCP套接字出错
- jquery - 如何解决 Uncaught TypeError: Cannot read property '0' of undefined for random quote generator?
- resteasy - Quarkus 多文件上传
- ios - SwiftUI 2 生命周期 - 处理自定义 URL 方案
- python - 使用错误输入时出现循环值错误
- android - 检索孩子的其他值
- python - 如何使用 Q 对象应用条件 AND 语句
- scala - 在 Scala 中继续 for 循环
- r - 有没有一种聪明的方法可以将 R 宽数据收集到需要收集数百个不同变量的长数据?
- flutter - 如何使用按钮转到特定页面?扑