【问题标题】:Node.js is not creating a directoryNode.js 没有创建目录
【发布时间】:2019-03-26 16:31:39
【问题描述】:

当我在终端上通过节点 file-create-directory.js 调用此文件时,我在名为 file-create-directory.js 的文件中有以下代码给我一个错误 我发现它与 fs.exists 模块有关,但我无法弄清楚

fs.js:140 抛出新的 ERR_INVALID_CALLBACK(); TypeError [ERR_INVALID_CALLBACK]:回调必须是可能回调的函数(fs.js:140:9) 在 Object.fs.exists (fs.js:218:3)

const fs = require('fs');



if(!fs.exists("views")) {

fs.mkdir("views", (err)=>{

   if(err) return err;

    fs.writeFile("./views/new.html", 'this is a new directory and data', (err)=>{

        if(err) return err;

       console.log('Directory and file saved')

    })


});

}

【问题讨论】:

  • fs.exists 已弃用,因此请改用此方法:nodejs.org/api/fs.html#fs_fs_existssync_path
  • 我看到 fs.existsSync 有效,我不需要异步,这对我有帮助,但是如果我需要在不阻塞的情况下检查它,我可以使用其他功能吗?
  • 你可以使用fs.access()。

标签: node.js file directory


【解决方案1】:

您正在使用内置“fs”模块的“exists”功能,它应该得到一个回调。 如果您想在没有回调的情况下同步执行它,您应该使用“existsSync”。

Documentation

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2014-04-16
    • 2019-11-12
    • 2014-02-05
    • 2016-07-29
    相关资源
    最近更新 更多