简体   繁体   English

替代fs.readFileSync()

[英]Alternative to fs.readFileSync()

I'm importing a css file into my npm module with Insert-css but i get an error. 我正在使用Insert-css将css文件导入我的npm模块,但是出现错误。

var fs = require('fs');
var inserCss = require('insert-css');
var css = fs.readFileSync(__dirname + '../css/file.css');
insertCss(css);

and the error I'm getting is 我得到的错误是

"Uncaught TypeError: fs.readFileSync is not a function" “未捕获的TypeError:fs.​​readFileSync不是函数”

Is there any alternative for fs.readFileSync? fs.readFileSync是否有其他选择? maybe npm module "Path"? 也许npm模块“路径”?

I created Node-Cheat for sync and async file reading see following working code: 我创建了Node-Cheat用于同步和异步文件读取,请参见以下工作代码:

//------------------------------------------------------
//fs module to read file in sync and async way
//Web Link=> https://github.com/zishon89us/node-cheat/blob/master/files/read_file.js
//------------------------------------------------------


var fs = require('fs'),
    filePath = './sample_files/sample_css.css';

// this for async way
/*fs.readFile(filePath, 'utf8', function (err, data) {
    if (err) throw err;
    console.log(data);
});*/

//this is sync way
var css = fs.readFileSync(filePath, 'utf8');
console.log(css);

Node Cheat Available at read_file with sample css file. 节点作弊可在read_file中使用示例css文件获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM