错误:fs.readFileSync不是函数 [英] Error: fs.readFileSync is not a function
问题描述
我正在尝试通过我制作的Chrome应用程序将十六进制文件闪存到Arduino.我使用了下面的基本NodeJS代码,并使用browserify对其进行了转换,以便可以在浏览器中使用.它不起作用,并且在控制台中出现错误,提示"TypeError:fs.readFileSync不是函数"
I am trying to flash a hex file to an Arduino through a Chrome app I am making. I used basic NodeJS code, which is below, and converted it using browserify so that it would work in the browser. It doesn't work and I'm getting an error in the console saying 'TypeError: fs.readFileSync is not a function'
var Avrgirl = require('avrgirl-arduino');
var avrgirl = new Avrgirl({
board: 'uno'
});
avrgirl.flash('Blink.cpp.hex', function (error) {
if (error) {
console.error(error);
} else {
console.info('done.');
}
});
推荐答案
您不能在浏览器中运行这种类型的代码(即使使用browserify).您应该在node.js中运行它.
You can't run this type of code in a browser (even with browserify). You should be running it in node.js.
我猜你的错误来自内部:
I would guess that your error is coming from inside of:
avrgirl.flash('Blink.cpp.hex', ...)
因为您要传递文件名,并且该函数可能试图读取该文件.
since you're passing a filename and that function is likely trying to read that file.
browserify
无法神奇地赋予浏览器强大的功能,如从硬盘驱动器中读取文件或与arduino硬件进行通讯所不具备的功能.该代码可能应该在node.js环境中运行.
browserify
cannot magically give a browser powers that it otherwise would not have such as reading files from your hard drive or communicating with arduino hardware. This code was probably meant to be run in the node.js environment.
avrgirl-arduino
模块的说明明确指出您应该在node.js中运行.
The instructions for the avrgirl-arduino
module, pretty clearly state you should be running in node.js.
这篇关于错误:fs.readFileSync不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!