首页 > 解决方案 > fs.open 没有在 nodejs 中打开所需的文件

问题描述

我只是 nodejs 的初学者,我遇到了 fs 模块。我试着写了几行代码。

var fs = require('fs'); 

// Open file demo.txt in read mode 
fs.open('/users/Upwn/desktop/ashok_pant.txt', 'r+', function (err, f) { 
  if(err) throw err; 
   console.log('Opened!'); 
}); 

输出是“打开!” 但我的本地文件系统没有打开任何内容。这可能有什么问题?

标签: javascriptnode.js

解决方案


如果您想使用链接到文件类型的默认 OS 应用程序打开文件,Philipp Kief提供的以下答案可能就是您想要的。

您可以使用开放模块:

npm install --save open

然后在你的 Node.js 文件中调用它:

const open = require('open');
open('my-file.txt');

该模块已经包含检测操作系统的逻辑,并且它运行 > 默认程序,该程序与您的系统关联到此文件类型。

--- https://stackoverflow.com/a/61891139/1487756


推荐阅读