【问题标题】:(NODE JS) Getting Error : ReferenceError: fs is not defined(NODE JS) 出现错误:ReferenceError: fs is not defined
【发布时间】:2021-01-16 03:54:39
【问题描述】:

创建了一个名为index.js的文件,代码为

var x = require("fs");
console.log("Hello");
fs.writeFileSync("text.txt","Hello");

当我运行它时,我收到一个错误:

ReferenceError: fs is not defined
at Object.<anonymous> (G:\web\Node Js\index.js:3:1)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47

【问题讨论】:

  • 您已将 fs 模块分配给变量 x do x.writeFileSync("text.txt","Hello");

标签: javascript node.js nodes node-modules web-deployment


【解决方案1】:

问题是由变量不匹配引起的。 您需要变量 x 中的模块并尝试使用未定义的变量 fs 访问该模块。

您必须使用相同的变量名。

var fs = require("fs");
console.log("Hello");
fs.writeFileSync("text.txt","Hello");

or

var x = require("fs");
console.log("Hello");
x.writeFileSync("text.txt","Hello");

【讨论】:

    【解决方案2】:

    应该是var fs = require('fs');

    Writing files in Node.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2020-05-05
      • 2022-10-06
      相关资源
      最近更新 更多