Node red doesn't restart after npm audit fix

Hi to all,
I've a big big problem, PLEASE HELP :sob: i'm desperate,
it's about some month that I work on a project and.... I've not flow backup

the story is:
I need to use moment inside a function node and after looking on node red forum I've found https://discourse.nodered.org/t/using-moment-in-function-node/25651

I decided to try and searched node-red setting.js to add moment:require('moment')
the npm install moment (on the same dir)

after that npm says

found 6 vulnerabilities (2 low, 2 moderate, 2 high) in 338 scanned packages
  run `npm audit fix` to fix 4 of them.
  1 vulnerability requires semver-major dependency updates.
  1 vulnerability requires manual review. See the full report for details.

like and idiot i run npm audit fix on the same dir and I made the omelet :sob: :sob: :sob:

now node-red doesn't start and if I try manually to start says:

Starting as a systemd service.
/usr/lib/node_modules/node-red/red.js:326
}).otherwise(function(err) {
   ^
TypeError: undefined is not a function
    at Object.<anonymous> (/usr/lib/node_modules/node-red/red.js:326:4)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47
nodered.service: Main process exited, code=exited, status=1/FAILURE
nodered.service: Failed with result 'exit-code'.
nodered.service: Service RestartSec=100ms expired, scheduling restart.
nodered.service: Scheduled restart job, restart counter is at 61.
Stopped Node-RED graphical event wiring tool.
Started Node-RED graphical event wiring tool.
/usr/lib/node_modules/node-red/red.js:326
}).otherwise(function(err) {
   ^
TypeError: undefined is not a function
    at Object.<anonymous> (/usr/lib/node_modules/node-red/red.js:326:4)
    at Module._compile (internal/modules/cjs/loader.js:1158:30).......

recursively.

I don't know what can I do to restore.
Someone can help me please???

Thanks a lot.
D.

Well make a backup of the flows file immediately, the errors are nothing to do with your actual flow, but are due to installed node issues. Back up everything in the .node-red directory except node_modules.

I suggest you start by removing the node_modules folder and also remove package-lock.json (not package.json). Then go into your .node-red folder and run
npm install
That will reinstall all the nodes back to the version in package.json (it may take a while on a low power machine). If you still have problems then post package.json here and tell us what the problem is. Also tell us what hardware/OS you are running on and in a terminal run
node-red
and post the full output here.

I'm on RPi 3 with Raspbian OS v10.7
node-red v1.0.4
node v12.16.1
npm v6.13.4

now i've seen that .node-red folder is under /home/pi.

changes I made are on /usr/lib/node_modules/node-red and i execute (with sudo) npm command on this prompt.

I suggest you start by removing the node_modules folder and also remove package-lock.json (not package.json).

Ok, I made it

 Then go into your .node-red folder and run
npm install

in my .node-red folder under /home/pi.....
or
under /usr/lib/node_modules/node-red ???

You didn't make that clear, in that case it is node red itself you have messed up, so my suggestions will not help.
Instead run the node red install/upgrade script from the node red docs for installing on the pi, that should reinstall node red and probably fix it.

OK @Colin
I made it with

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

after few minutes later all is done.

Node-red is restarted, now i'm going to re-install all missing modules of my project.

thanks

Running npm install in the .node-red folder (as previously suggested) should have done that for you.

I made it and (two times, one as pi user one as root) and it resolve many modules but not all, get some error because some modules is not updated and not compatible with new node-red (serialport i.e.)
and this is result...


MySQLdatabase
ui_tab
ui_base
ncd-comm
ui_group
ui_spacer
ui_template
camerapi-takephoto
ui_gauge
ui_text
ui_text_input
ui_dropdown
mysql
moment
ui_date_picker
ui_button
join-wait
ncd-ads1115
base64
ui_led
ui_switch
ui_chart
ui_text-input
ui_ui_control
InvertUltimate
BooleanLogicUltimate```

now I try manually or you suggest me to try " npm install" on /usr/lib/node_module/node-red ?

The only thing you should do as root is to install global modules (using npm install -g) which there is generally no need to do.

What did you do as root?

Absolutely not. In your .node-red folder (usually /home/pi/.node-red) which is where the flows file is. Where did you get the idea that you should do anything in /usr/lib? Don't.

I did it twice because the first time i did as user pi "npm install" then I got errors, I thought there were errors because I had to do as root.

I execute
/home/pi/.node-red# sudo npm install

as I said in the first msg in the dir / usr / lib / ......
I placed in the setting.js file "moment: require ('moment')
execute npm install moment

and execute "npm audit fix"
because npm said to run it to fix security issues

1 Like

I re-install modules one by one from node-red palette,
I erase modules that was unnecessary for my project
and now i'm working.

many many thanks for your help. :pray: :pray: :pray:

You are not the first to be messed up by running npm audit. Those messages are not something the node-red team has any control over.

Having used sudo with npm for local nodes you may find you have files under your home directory owned by root which may cause you problems installing or removing nodes in the future. If you do then you need to set all files in your home directory back to being owned by the node-red user (probably pi).

yes, some modules had root:root as owner:group, I change this like the others pi:pi but....
dashboard doesn't work, when I open localhost:1880/ui it open screen with this like the first time

Welcome to the Node-RED Dashboard
Please add some UI nodes to your flow and redeploy.

I think when (out of ignorance) I ran "npm audit fix" under "/usr/lib/node_module/node-red" I shit and now, I can see the flow, I have all nodes configured as before, tab and group of the dashboards are in place but the dashboard does not start.

inside the dir "/usr/lib/node_module" the dir "node-red" has disappeared

moreover, before to enter node-red and the dashboard I used the username and password that I had set in the node-red configuration, now there is no longer the request.

:sob:

SOLVED.

problem of dashboard was that I had ncd-comm module not installed but a configuration in a flow that used it and deploy stop before to start.
I deleted this and now I'm really working.

TWo learning points here:

  1. NEVER run npm audit fix unless you REALLY know what you are doing. It will nearly always break something.

  2. If you get totally in a mess with installed modules, don't forget that you can always delete the node_modules folder as long as you have a matching package.json file that contains the module details. Then you can simply do an npm install and everything will be restored.

A bonus 3rd learning point is that it is highly recommended that you learn the basics of npm and the file/folder structures it produces. It doesn't take much and it will really help you out in the future.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.