首页 > 解决方案 > 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 解决这个问题?

标签: reactjswebpackwinston

解决方案


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"});

推荐阅读