启动带有 NPM 模块 "file-exists" 的 Node-RED 时出现意外令牌错误

Unexpected token error when starting Node-RED with NPM module "file-exists" implemented

我目前正在 Node-RED 中开发一个应用程序,我想使用 NPM 中的一些模块。 James Thom 的这个模块可以自动执行我可以使用的 NPM 模块 (node-red-contrib-npm)。

但是,我想首先进行手动实施,以更彻底地 node.js 并更好地理解它的工作原理(我是 node.js 的新手)。因此,我没有使用该模块,而是按照官方 Node-RED 指南所说 here:

  1. 相应地编辑 settings.js 文件中的 functionGlobalContext 属性,例如使用 ccount NPM 包:

functionGlobalContext: {
    // os:require('os'),
    // octalbonescript:require('octalbonescript'),
    // jfive:require("johnny-five"),
    // j5board:require("johnny-five").Board({repl:false})
// fileExists:require("file-exists") //DOESN'T WORK!!
ccount:require('ccount') //WORKS FINE!!
},

  1. 在 Node-RED 函数节点中,我输入:

var ccount = context.global.get('ccount');

msg.a = ccount('((())))))', '(') //=> 2
msg.b = ccount('pepe', ')') //=> 1

return msg;

  1. 帐户工作正常。现在,我根据 README.md 文件中的示例代码对 file-exists 执行相同的操作:

    functionGlobalContext: {
        // os:require('os'),
        // octalbonescript:require('octalbonescript'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
 fileExists:require("file-exists") //DOESN'T WORK!!
 // ccount:require('ccount') //WORKS FINE!!
    },

  1. 在 Node-RED 中,我在函数块中输入:

fileExists('/media/pi/SMOOTHIE/firmware.cur', (err, exists) => console.log(exists)) // OUTPUTS: true or false

console.log(fileExists.sync('/media/pi/SMOOTHIE/firmware.cur')) // OUTPUTS: true or false

return msg;

当我执行node-red-start时,控制台的输出是:

pi@raspberrypi:~ $ node-red-start

Start Node-RED

Once Node-RED has started, point a browser at http://192.168.1.104:1880
On Pi Node-RED works better with the Firefox or Chrome browser

Use   node-red-stop                          to stop Node-RED
Use   node-red-start                         to start Node-RED again
Use   node-red-log                           to view the recent log output
Use   sudo systemctl enable nodered.service  to autostart Node-RED at every boot
Use   sudo systemctl disable nodered.service to disable autostart on boot

Starting as a systemd service.
Started Node-RED graphical event wiring tool..
Error loading settings file: /home/pi/.node-red/settings.js
[SyntaxError: Unexpected token =]

node-red-log 输出这个:

13 Sep 19:29:20 - [info] Starting flows
13 Sep 19:29:20 - [info] Started flows
13 Sep 19:29:20 - [info] serial port /dev/ttyACM0 opened at 57600 baud 8N1
13 Sep 19:30:25 - [info] Stopping flows
13 Sep 19:30:25 - [info] serial port /dev/ttyACM0 closed
13 Sep 19:30:25 - [info] Stopped flows
13 Sep 19:30:25 - [info] Starting flows
13 Sep 19:30:25 - [info] Started flows
13 Sep 19:30:25 - [info] serial port /dev/ttyACM0 opened at 57600 baud 8N1
Stopping Node-RED graphical event wiring tool....
13 Sep 19:31:54 - [info] Stopping flows
Stopped Node-RED graphical event wiring tool..
Started Node-RED graphical event wiring tool..
Error loading settings file: /home/pi/.node-red/settings.js
[SyntaxError: Unexpected token =]
Started Node-RED graphical event wiring tool..
Started Node-RED graphical event wiring tool..
Error loading settings file: /home/pi/.node-red/settings.js
[SyntaxError: Unexpected token =]
Started Node-RED graphical event wiring tool..
Error loading settings file: /home/pi/.node-red/settings.js
[SyntaxError: Unexpected token =]
Started Node-RED graphical event wiring tool..
Error loading settings file: /home/pi/.node-red/settings.js
[SyntaxError: Unexpected token =]

我没有其他要检查的东西。该过程很好,因为它适用于其他 npm 节点。 file-exists 模块 (https://www.npmjs.com/package/file-exists) 是由麻省理工学院的一些人制作和维护的,每天大约有 8 万次下载,所以我相信它应该可以工作。 GitHub 回购目前没有提到其他问题的错误。我可能做错了什么?

谢谢。

编辑:节点-e的输出"require('./settings.js');"

pi@raspberrypi:~/.node-red $ node -e "require('./settings.js');"
/home/pi/.node-red/node_modules/file-exists/index.js:4
function fileExists (filepath, options, done = function () {}) {
                                         ^

SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/home/pi/.node-red/settings.js:179:13)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
pi@raspberrypi:~/.node-red $ 

文件存在节点需要节点 v6.x 或更新版本,错误是因为它使用了节点 v4.8.x 不理解的新语法。

您可以使用 here 中的 Node-RED 升级脚本,它将 Node-RED 升级到最新版本并将 NodeJS 升级到最新的 v6 版本