I messed up installation

When I installed NodeRED, the whole thing was quite new to me: node.js, npm etc. Over the time I've made a mess with my installation - I was installing/upgradign nodes with NodeRED web interface, npm with and without -g, as sudo and local user in different directories. As the result, I cannot update most of the nodes any more and have lots of errors when I try to install or update. I have working flow using storage to retain my data over the months and I don't want to lose it. Can you guys help me sort this out? How to clean up this mess without loosing my flows and data? Would it be better to run NodeRED in docker? I always preferred to run it locally. Cheers.

What hardware is it running on?
Operating system details?

Hi Paul-Reed, it's Ubuntu, some old 4 core AMD64, I think, 8GB RAM, 200GB SSD (system) plus some standard HDD for storage.

Example of one of the errors:

"Installation of module node-red-contrib-modbus failed:"
28/09/2019, 22:27:44msg : string[42]
"------------------------------------------"
28/09/2019, 22:27:44msg : string[1209]
string[1209]

node-red-project@0.0.1 /home/user/.node-red
├── node-red-contrib-telegrambot-plus@1.10.0 
└── node-red-node-mysql@0.0.17 

npm WARN enoent ENOENT: no such file or directory, open '/home/user/.node-red/node_modules/pg/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/home/user/.node-red/node_modules/postgrestor/package.json'
npm WARN node-red-project@0.0.1 No repository field.
npm WARN node-red-project@0.0.1 No license field.
npm ERR! Linux 4.15.0-58-generic
npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "--save" "--save-prefix=~" "--production" "node-red-contrib-modbus@4.1.3"
npm ERR! node v8.10.0
npm ERR! npm  v3.5.2
npm ERR! path /home/user/.node-red/node_modules/node-red-contrib-modbus
npm ERR! code ENOTEMPTY
npm ERR! errno -39
npm ERR! syscall rename

npm ERR! ENOTEMPTY: directory not empty, rename '/home/user/.node-red/node_modules/node-red-contrib-modbus' -> '/home/user/.node-red/node_modules/.node-red-contrib-modbus.DELETE'
npm ERR! 
npm ERR! If you...

28/09/2019, 22:27:44
msg : string[42]
"--------------------------------

But, when I run in terminal: npm -g install node-red-contrib-modbus the installation is succesfull, sudo service node-red restart and check the flows: node-red-contrib-modbus is still out of date and fails to update using web interface

One thing I see is that your npm version is quite old. Unsure if related, just something I spotted. Also, try reading up on global installs of packages versus local installs. It looks like your attempts to fix it are creating an even bigger mess. Might be best to start over from scratch: backup your flows (see user dir/project directories) delete nodejs, delete node-red, reinstall using the script in the docs (the pi script is made to work on ubuntu too), then try to only install nodes through the palette, or when needed to install a specific version follow the given instructions carefully.

@afelix, I got this feeling that I try to update npm and even this fails, well, installation is success but when I check version, it's still on the old one. What is the right procedure here?
And if I got with removing the whole thing all together, what I need to remove and where I need to look to make sure there are no remains of it?
Cheers.

Start with a which node to figure out where it was installed and how, for example locally with a script or through apt-get. Then deinstall the same way. Manually delete node_modules folders, as well as ~/.npm while you’re still at it. If that one fails to delete regularly, try with sudo as it contains the nodes installation cache and might start causing problems after installing libraries with sudo.

Backup your flows and flows_creds files, plus package.json in the node-red user dir (see the startup log for NR), as well as the projects folder(s) and persistent context folders if relevant. If you have a good backup, delete the nr user dir too. Again sudo delete if things got messed up there too but an rm -r should get rid of it normally. Sudo is only needed if it got messed up through sudo too.

Next, start reading here: https://nodered.org/docs/getting-started/raspberrypi
Then read the installing nodes guide: https://nodered.org/docs/user-guide/runtime/adding-nodes
Remember that when using npm to install nodes, for them to be picked up by NR you have to be in the user directory, and install them without the -g option.

It looks like it's a good time to do it as the NodeRed just bumped into version 1.0
I did upgrade on my test machine with Docker and apart from the file access rights to the data folder it was nice and smooth. Can wait, when I sort my main installation out. It's just so frightening, when you've got quite a big, working diagram and have to perform uninstall->install :worried:

Tried tonight, after running script for raspbery-pi or manual install with npm, this is a result of node-red-start:

Node-RED is not yet fully installed
/usr/bin/node-red-start: line 91: update-nodejs-and-nodered: command not found

No errors in /var/log/nodered-install.log

using the script from here ? https://nodered.org/docs/getting-started/raspberrypi
what does node -v report and which node and npm -v
And which version of Pi is this ?

Yes, I run it on Ubuntu not RPi as "If you are on a Raspberry Pi or any Debian-based operating system, including Ubuntu and Diet-Pi, you can use the Pi install script", just chose "no" to "install raspberry pi modules":

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


This script will remove versions of Node.js prior to version 7.x, and Node-RED and
if necessary replace them with Node.js 10.x LTS (dubnium) and the latest Node-RED from Npm.

It also moves any Node-RED nodes that are globally installed into your user
~/.node-red/node_modules directory, and adds them to your package.json, so that
you can manage them with the palette manager.

It also tries to run 'npm rebuild' to refresh any extra nodes you have installed
that may have a native binary component. While this normally works ok, you need
to check that it succeeds for your combination of installed nodes.

To do all this it runs commands as root - please satisfy yourself that this will
not damage your Pi, or otherwise compromise your configuration.
If in doubt please backup your SD card first.

Are you really sure you want to do this ? [y/N] ? y

Would you like to install the Pi-specific nodes ? [y/N] ? n

Running Node-RED update for user adam at /home/adam on ubuntu


This can take 20-30 minutes on the slower Pi versions - please wait.

  Stop Node-RED                       ✔
  Remove old version of Node-RED      ✔
  Remove old version of Node.js       ✔
  Install Node.js LTS                 ✔   Node v10.16.3   Npm 6.9.0
  Clean npm cache                     ✔
  Install Node-RED core               ✔   1.0.0
  Move global nodes to local          -
  Install extra Pi nodes              -
  Npm rebuild existing nodes          -
  Add shortcut commands               ✔
  Update systemd script               ✔


Any errors will be logged to   /var/log/nodered-install.log
All done.
  You can now start Node-RED with the command  node-red-start
  or using the icon under   Menu / Programming / Node-RED
  Then point your browser to localhost:1880 or http://{your_pi_ip-address}:1880

Started  Tue  1 Oct 22:48:42 BST 2019  -  Finished  Tue  1 Oct 22:50:31 BST 2019

adam@linux-pc:~$ no
node              nodejs            node-red-log      node-red-restart  node-red-start    node-red-stop     nohup             nologin           notify-send
adam@linux-pc:~$ node-red-start

Node-RED is not yet fully installed
/usr/bin/node-red-start: line 91: update-nodejs-and-nodered: command not found
adam@linux-pc:~$ node -v
v10.16.3
adam@linux-pc:~$ which node
/usr/bin/node
adam@linux-pc:~$ npm -v
6.9.0

Ah right - and you have a 32 bit processor ?
what does cat /proc/cpuinfo give ?
I think the issue is the processor is 32bit x86 ?

It's an AMD Athlon II X4 64bit.
I figured out that if I run .npm-global/bin/node-red it starts

which version of nodejs and npm ? node -v && npm -v

node: v10.16.3
npm: 6.9.0

apologies - yes missed that from above.... ok - this is most odd.
what does which node-red report ?

This does nothing - no output but which node-red-start produces /usr/bin/node-red-start

OK - baffled now - the actual core install should create the node-red command in /usr/bin directory... which should just be a link to ../lib/node_modules/node-red/red.js , which should of course be executable.

Alright, how do I tide up this thing right?

We could try to redo just that bit...
sudo npm i -g --unsafe-perm node-red
which should just do the main install and create the command/link. (or you could recreate the link manually) which would be sudo ln -s /usr/lib/node_modules/node-red/red.js /usr/bin/node-red