【问题标题】:Getting error while reading local file in node.js在 node.js 中读取本地文件时出错
【发布时间】:2013-08-01 03:58:02
【问题描述】:

我正在尝试使用 node.js 读取本地文件。我的 NPM 模块试图执行一个子进程,然后打开一个文件来读取。阅读此内容时会引发错误

{ [Error: ENOENT, open 'E:\Project\SecIntegrator\Attack\manifest.json']
  errno: 34,
  code: 'ENOENT',
  path: 'E:\\Project\\SecIntegrator\\Attack\\manifest.json' }

读取这个文件的实际路径是

E:\Project\SecIntegrator\node_modules\restScannerDriver\GarudRudra\Attack\manifest.json

我已使用 var configPath = path.join(path.dirname(fs.realpathSync(__filename)), '/'); 计算绝对路径,但仍然无法正常工作。

【问题讨论】:

  • 你用的是哪种阅读方式?
  • file.readFile(fileName, 'utf8', function (err,data) {}
  • 不确定,但您的路径看起来像 Windows 路径,所以请看这里stackoverflow.com/questions/10015877/…
  • 如果我从不同的命令提示符运行代码作为独立代码,它工作正常。当我将它集成为 NPM 模块时,它无法正确地走路径。

标签: node.js file-io npm


【解决方案1】:

在 NPM 模块中,您的路径会发生变化。 所以尝试在 NPM 中使用 __dirname 并像这样创建真实路径:

var filepath = fs.realpathSync(__dirname+'/'+relative path from here to your file);

然后看看文件路径的结果是什么

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2015-08-11
    相关资源
    最近更新 更多