错误:fs.readFileSync 不是函数 [英] Error: fs.readFileSync is not a function

查看:44
本文介绍了错误:fs.readFileSync 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过我正在制作的 Chrome 应用程序将十六进制文件闪存到 Arduino.我使用了下面的基本 NodeJS 代码,并使用 browserify 对其进行了转换,以便它可以在浏览器中运行.它不起作用,我在控制台中收到一条错误消息,提示TypeError: fs.readFileSync is not a function"

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆