Use fs in typescript

前端 未结 2 856
醉酒成梦
醉酒成梦 2021-01-03 17:45

I\'m just trying to read a file using fs.readFileSync, though it seems it cannot be found.

I made sure to declare it, added it within my constructor:

2条回答
  •  天命终不由人
    2021-01-03 18:11

    Using node -v 10.15.0 and @types/node:

    It seems declaration has been rewritten...

    fs definition is declared as a module so you should do:

    import fs from "fs"; // Without star

    compiled:

    var fs_1 = __importDefault(require("fs"));

    or

    const fs = require("fs"); instead of require("fs").default;

    with star you will have fs.default.TheFunctionYouWant instead of fs.TheFunctionYouWant

    The better way is to console.log(fs); to see what it is imported.

    {
      "compilerOptions": {
        "typeRoots": [],
        "types": [
          "node"
        ]
      }
    }
    

提交回复
热议问题