Search code examples
javascriptnode.jsnpmmodulenode-red

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


I am currently developing an application in Node-RED and I'd like to use some modules from NPM. There's this module from James Thom that automates the implementation of NPM modules (node-red-contrib-npm) that I could use.

However, I'd like to make a manual implementation at first to node.js more throughly and better understand how it works (I'm new to node.js). So, instead of using that module, I do as the official Node-RED guide says here:

  1. Edit the functionGlobalContext property in the settings.js file accordingly, e.g. with the ccount NPM package:

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. In a Node-RED function node I put:

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

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

return msg;

  1. ccount works fine. Now I do the same with file-exists as per the example code in its README.md file says:

    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. In Node-RED, in a function block I put:

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;

When I do node-red-start, the console's output is:

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 outputs this:

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 =]

I have nothing else to check. The procedure is fine because it worked for the other npm node. The file-exists module (https://www.npmjs.com/package/file-exists) was made and is maintained by some guys at MIT and gets like 80k downloads per day, so I believe it should work. It's GitHub repo mentions no bugs other issues at this time. What could I be doing wrong?

Thank you.

EDIT: Output of node -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 $ 

Solution

  • The file-exists node required Node v6.x or newer, the error is because it is using a new syntax that node v4.8.x does not understand.

    You can use the Node-RED upgrade script from here which will upgrade Node-RED to the latest version and upgrade NodeJS to the latest v6 build