Could not find user directory. Ensure $HOME is set for the current user, or use --userDir option

Hi everyone!
I recently upgraded Node-Red for Raspberry PI4. It may be that I did something wrong, so Node-Red cannot run automatically. I typed 'node-red-start' in the command window(or click Application Menu --Programming -- node red) and got the following message:

Could not find user directory. Ensure $HOME is set for the current user, or use --userDir option
nodered.service: Main process exited, code=exited, status=1/FAILURE
nodered.service: Failed with result 'exit-code'.
nodered.service: Consumed 1.422s CPU time.
nodered.service: Scheduled restart job, restart counter is at 6.
Stopped Node-RED graphical event wiring tool.
nodered.service: Consumed 1.422s CPU time.
Started Node-RED graphical event wiring tool.

Node red does not work properly. When I type 'node-red' in the command window, I get the following information:

> Welcome to Node-RED
> ===================
> 5 Oct 03:36:39 - [info] Node-RED version: v3.0.2
> 5 Oct 03:36:39 - [info] Node.js version: v12.22.12
> 5 Oct 03:36:39 - [info] Linux 5.15.61-v7l+ arm LE
> 5 Oct 03:36:41 - [info] Loading palette nodes
> 5 Oct 03:36:43 - [info] Dashboard version 3.1.6 started at /ui
> 5 Oct 03:36:43 - [info] Settings file : /home/pi/.node-red/settings.js
> 5 Oct 03:36:43 - [info] Context store : 'default' [module=memory]
> 5 Oct 03:36:43 - [info] User directory : /home/pi/.node-red
> 5 Oct 03:36:43 - [warn] Projects disabled : editorTheme.projects.enabled=false
> 5 Oct 03:36:43 - [info] Flows file : /home/pi/.node-red/flows.json
*> 5 Oct 03:36:43 - [warn] *
> ---------------------------------------------------------------------
> Your flow credentials file is encrypted using a system-generated key.
> If the system-generated key is lost for any reason, your credentials
> file will not be recoverable, you will have to delete it and re-enter
> your credentials.
> You should set your own key using the 'credentialSecret' option in
> your settings file. Node-RED will then re-encrypt your credentials
> file using your chosen key the next time you deploy a change.
> ---------------------------------------------------------------------

Node red can run normally. I use 'sudo systemctl enable nodered.service 'again, but node red cannot run automatically when the system is powered on. I must enter node red again in the command window. I am a beginner and do not know how to do. I need help. Thanks in advance!

Did you install node-red using the recommended technique in the node-red docs?

Please run these commands and show us what they give
sudo systemctl status nodered.service
cat /lib/systemd/system/nodered.service
echo $HOME

I only noticed the correct way after upgrading node red, so I think it was a mistake.

'sudo systemctl status nodered.service' command

pi@raspberrypi:~ $ sudo systemctl status nodered.service
* nodered.service - Node-RED graphical event wiring tool
     Loaded: loaded (/lib/systemd/system/nodered.service; enabled; vendor prese>
     Active: activating (auto-restart) (Result: exit-code) since Wed 2022-10-05>
       Docs: http://nodered.org/docs/hardware/raspberrypi.html
    Process: 8543 ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OP>
   Main PID: 8543 (code=exited, status=1/FAILURE)
        CPU: 1.418s

'cat /lib/systemd/system/nodered.service' command

pi@raspberrypi:~ $ cat /lib/systemd/system/nodered.service
# systemd service file to start Node-RED

[Unit]
Description=Node-RED graphical event wiring tool
Wants=network.target
Documentation=http://nodered.org/docs/hardware/raspberrypi.html

[Service]
Type=simple
# Run as normal pi user - change to the user name you wish to run Node-RED as
User=
Group=
WorkingDirectory=/home/

Environment="NODE_OPTIONS=--max_old_space_size=512"
# define an optional environment file in Node-RED's user directory to set custom variables externally
EnvironmentFile=-/home//.node-red/environment
# uncomment and edit next line if you need an http proxy
#Environment="HTTP_PROXY=my.httpproxy.server.address"
# uncomment the next line for a more verbose log output
#Environment="NODE_RED_OPTIONS=-v"
# uncomment next line if you need to wait for time sync before starting
#ExecStartPre=/bin/bash -c '/bin/journalctl -b -u systemd-timesyncd | /bin/grep -q "systemd-timesyncd.* Synchronized to time server"'

ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
#ExecStart=/usr/bin/env node $NODE_OPTIONS red.js $NODE_RED_OPTIONS
# Use SIGINT to stop
KillSignal=SIGINT
# Auto restart on crash
Restart=on-failure
RestartSec=20
# Tag things in the log
SyslogIdentifier=Node-RED
#StandardOutput=syslog

[Install]
WantedBy=multi-user.target

‘echo $HOME‘’ command

pi@raspberrypi:~ $ echo $HOME
/home/pi
pi@raspberrypi:~ $

As you can see, in the service file the User and Group have not been set, because you did not run the script.
Run the script now and it should sort everything out.

Follow this link( Running on Raspberry Pi : Node-RED ), The script can download something from the link(https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)) according to the command(bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)) , but I can't link it here, so I can't perform the installation.

pi@raspberrypi:~ $ sudo -i

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

root@raspberrypi:~# sudo apt install build-essential git curl
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
build-essential is already the newest version (12.9).
curl is already the newest version (7.74.0-1.3+deb11u3).
git is already the newest version (1:2.30.2-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
174 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Setting up node-qs (6.9.4+ds-1) ...
Setting up libuv1-dev:armhf (1.40.0-1) ...
Setting up node-extsprintf (1.4.0-1) ...
Setting up libjs-inherits (2.0.4-1) ...
Setting up node-through (2.3.8+~cs0.0.30-1) ...
Setting up node-color-name (1.1.4+~1.1.1-1) ...
Setting up node-uuid (8.3.2+~8.3.0-4) ...
Setting up nodejs (12.22.12~dfsg-1~deb11u1) ...
update-alternatives: using /usr/bin/nodejs to provide /usr/bin/js (js) in auto m
ode
Setting up node-aws4 (1.11.0-1) ...
Setting up node-indent-string (4.0.0-1) ...
Setting up node-function-bind (1.1.1+repack-1) ...
Setting up node-clone (2.1.2-2) ...
Setting up node-iferr (1.0.2-1) ...
Setting up node-chownr (1.1.3-5) ...
Setting up node-has-flag (4.0.0-1) ...
Setting up libjs-psl (1.8.0+ds-4) ...
Setting up node-json-schema-traverse (1.0.0-2) ...
Setting up node-asap (2.0.6-2) ...
Setting up node-mime (2.5.0+dfsg+~cs3.90.0-1) ...
Setting up node-inherits (2.0.4-1) ...
Setting up node-path-is-absolute (2.0.0-1) ...
Setting up node-universalify (2.0.0-2) ...
Setting up node-ini (2.0.0-1) ...
Setting up node-iconv-lite (0.5.1-3) ...
Setting up node-safe-buffer (5.2.1+~cs2.1.2-1) ...
Setting up node-promise-inflight (1.0.1-1.1) ...
Setting up node-json-stringify-safe (5.0.1+repack-3) ...
Setting up node-json-parse-better-errors (1.0.2+~2.3.1-1) ...
Setting up node-fast-deep-equal (3.1.3-1) ...
Setting up node-har-schema (2.0.0-4) ...
Setting up node-mime-types (2.1.28-1) ...
Setting up node-performance-now (2.1.0+debian-1.1) ...
Setting up node-err-code (2.0.3+dfsg-1) ...
Setting up node-resolve-from (5.0.0+~3.1.0+~3.3.0+~2.0.0-1) ...
Setting up node-balanced-match (1.0.0-1) ...
Setting up node-ansi (0.3.1-1) ...
Setting up node-brace-expansion (2.0.0-1) ...
Setting up node-spdx-exceptions (2.3.0-1) ...
Setting up node-set-blocking (2.0.0-1.1) ...
Setting up libssl-dev:armhf (1.1.1n-0+deb11u3+rpt1) ...
Setting up node-tunnel-agent (0.6.1-2) ...
Setting up node-npm-bundled (1.1.1-1) ...
Setting up node-signal-exit (3.0.3-1) ...
Setting up node-wrappy (1.0.2-1.1) ...
Setting up node-text-table (0.2.0-2) ...
Setting up node-asynckit (0.4.0-3) ...
Setting up node-ip (1.1.5-5) ...
Setting up node-punycode (2.1.1-3) ...
Setting up node-defaults (1.0.3-2) ...
Setting up node-mute-stream (0.0.8-2) ...
Setting up libjs-typedarray-to-buffer (4.0.0-1) ...
Setting up node-number-is-nan (2.0.0-1) ...
Setting up node-graceful-fs (4.2.4+repack-1) ...
Setting up node-jsonify (0.0.0-1.1) ...
Setting up node-json-stable-stringify (1.0.1+~cs5.1.32-1) ...
Setting up node-aproba (2.0.0-1) ...
Setting up node-ip-regex (4.3.0-1) ...
Setting up node-spdx-license-ids (3.0.7-1) ...
Setting up node-string-decoder (1.3.0-2) ...
Setting up node-mkdirp (1.0.4+~1.0.1-1) ...
Setting up node-run-queue (2.0.0-1) ...
Setting up node-core-util-is (1.0.2-2) ...
Setting up node-json-schema (0.3.0+~7.0.6-1+deb11u1) ...
Setting up node-uri-js (4.4.0+dfsg-5) ...
Setting up node-minimatch (3.0.4+~3.0.3-1) ...
Setting up node-opener (1.5.2-1) ...
Setting up node-archy (1.0.0-3) ...
Setting up node-caseless (0.12.1-1) ...
Setting up node-imurmurhash (0.1.4-1.1) ...
Setting up node-encoding (0.1.13-1) ...
Setting up node-read (1.0.7-2) ...
Setting up node-asn1 (0.2.3-2) ...
Setting up node-color-convert (1.9.3-1) ...
Setting up node-isarray (2.0.5-1) ...
Setting up node-osenv (0.1.5-1) ...
Setting up node-ms (2.1.3+~cs0.7.31-1) ...
Setting up libjs-is-typedarray (1.0.0-3) ...
Setting up node-process-nextick-args (2.0.0-1) ...
Setting up node-has-unicode (2.0.1-2) ...
Setting up node-fs-write-stream-atomic (1.0.10-4) ...
Setting up gyp (0.1+20200513gitcaa6002-2) ...
Setting up node-forever-agent (0.6.1-2) ...
Setting up node-delayed-stream (1.0.0-4) ...
Setting up node-isstream (0.1.2+dfsg-1.1) ...
Setting up node-builtins (1.0.3-2) ...
Setting up node-colors (1.4.0-1) ...
Setting up node-fs.realpath (1.0.0-1.1) ...
Setting up node-oauth-sign (0.9.0-2) ...
Setting up node-jsbn (1.1.0-1.1) ...
Setting up node-object-assign (4.1.1-3) ...
Setting up node-leven (3.1.0+~cs1.1.1-1) ...
Setting up node-abbrev (1.1.1-2) ...
Setting up node-puka (1.0.1+dfsg-2) ...
Setting up node-supports-color (8.1.0+~7.2.0-1) ...
Setting up node-yallist (4.0.0-1) ...
Setting up node-ansi-regex (5.0.1-1~deb11u1) ...
Setting up node-once (1.4.0-3) ...
Setting up node-psl (1.8.0+ds-4) ...
Setting up libnode-dev (12.22.12~dfsg-1~deb11u1) ...
Setting up node-slash (3.0.0-1) ...
Setting up node-util-deprecate (1.0.2-1) ...
Setting up node-retry (0.12.0-1) ...
Setting up node-concat-map (0.0.1-2) ...
Setting up node-assert-plus (1.0.0-2) ...
Setting up node-ansistyles (0.1.3-2) ...
Setting up node-delegates (1.0.0-2) ...
Setting up node-depd (2.0.0-1) ...
Setting up node-isexe (2.0.0-5) ...
Setting up node-resolve (1.19.0+~cs5.20.8-2) ...
Setting up node-jsonparse (1.3.1-7) ...
Setting up node-ajv (6.12.6-2) ...
Setting up node-tweetnacl (1.0.3+dfsg-1) ...
Setting up node-escape-string-regexp (4.0.0-1) ...
Setting up node-aws-sign2 (0.7.1-2) ...
Setting up node-console-control-strings (1.1.0-2) ...
Setting up node-debug (4.3.1+~cs4.1.5-1) ...
Setting up node-extend (3.0.2-1) ...
Setting up node-jsonstream (1.3.5-1) ...
Setting up node-dashdash (2.0.0-1) ...
Setting up node-p-map (4.0.0-1) ...
Setting up node-validate-npm-package-name (3.0.0-1.1) ...
Setting up node-promzard (0.3.0-1.1) ...
Setting up node-wcwidth.js (1.0.0-1.1) ...
Setting up node-ecc-jsbn (0.2.0-2) ...
Setting up node-combined-stream (1.0.8-1) ...
Setting up node-unique-filename (1.1.1+ds-1) ...
Setting up node-ansi-styles (4.2.1-1) ...
Setting up node-form-data (3.0.0-2) ...
Setting up node-strip-ansi (6.0.0-2) ...
Setting up node-lockfile (1.0.4-3) ...
Setting up node-chalk (4.1.0-1) ...
Setting up node-spdx-expression-parse (3.0.1-1) ...
Setting up node-which (2.0.2+~cs1.3.2-1) ...
Setting up node-is-typedarray (1.0.0-3) ...
Setting up node-tar (6.0.5+ds1+~cs11.3.9-1+deb11u2) ...
Setting up node-inflight (1.0.6-1.1) ...
Setting up node-tough-cookie (4.0.0-2) ...
Setting up node-verror (1.10.0-1.1) ...
Setting up node-getpass (0.1.7-1.1) ...
Setting up node-nopt (5.0.0-1) ...
Setting up node-string-width (4.2.0-1) ...
Setting up node-har-validator (5.1.5-1) ...
Setting up node-readable-stream (3.6.0-2) ...
Setting up node-ssri (8.0.1-2) ...
Setting up node-lru-cache (5.1.1-5) ...
Setting up node-bcrypt-pbkdf (1.0.2-1) ...
Setting up node-promise-retry (2.0.1-1) ...
Setting up node-columnify (1.5.4-3) ...
Setting up node-jsprim (2.0.0-1) ...
Setting up node-are-we-there-yet (1.1.5-1) ...
Setting up node-spdx-correct (3.1.1-1) ...
Setting up node-glob (7.1.6+~7.1.3-1) ...
Setting up node-typedarray-to-buffer (4.0.0-1) ...
Setting up node-hosted-git-info (3.0.8-1) ...
Setting up node-wide-align (1.1.3-1) ...
Setting up node-rimraf (3.0.2-1) ...
Setting up node-sshpk (1.16.1+dfsg-2) ...
Setting up node-semver (7.3.4-1) ...
Setting up node-validate-npm-package-license (3.0.4-1) ...
Setting up node-write-file-atomic (3.0.3+~3.0.1-1) ...
Setting up node-copy-concurrently (1.0.5-7) ...
Setting up node-move-concurrently (1.0.1-2) ...
Setting up node-agent-base (6.0.2-2) ...
Setting up node-http-signature (1.3.5-1) ...
Setting up node-gauge (2.7.4-1.1) ...
Setting up node-normalize-package-data (3.0.0+~2.4.0-1) ...
Setting up node-npm-package-arg (8.1.0-1) ...
Setting up node-request (2.88.1-5) ...
Setting up node-npmlog (4.1.2-2) ...
Setting up node-https-proxy-agent (5.0.0-3) ...
Setting up node-cacache (15.0.5+~cs13.9.21-1) ...
Setting up node-read-package-json (3.0.0-1) ...
Setting up node-gyp (7.1.2-4) ...
Setting up npm (7.5.2+ds-2) ...
Processing triggers for man-db (2.9.4-2) ...
root@raspberrypi:~# bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
root@raspberrypi:~# bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)
root@raspberrypi:~#

The command(--bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered) doesn't work in here.how do to?

Aargh! Don't do it as root. Don't do anything as root unless there is good reason.

Though that should not cause it to do nothing. If it doesn't work as the normal user, what happens if, in a browser, you go to https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered

You do seeem to have a misunderstanding about sudo:

In the first example, once you are the superuser (sudo -i) then there is no point in prefacing any further command with sudo.

Running commands as root when they don't need it, especially installations, leads to all sorts of problems later when you try and use the installed software.
For example, does the directory /home/pi/.node-red exist? Does it belong to pi or to root? ls -ld /home/pi/.node-red
If it belongs to root you may be able to fix it with sudo chown -R pi:pi /home/pi/.node-red
And you can probably fix the service file by editing it - this will need sudo!
The relevant bit is

[Service]
Type=simple
# Run as normal pi user - change to the user name you wish to run Node-RED as
User=pi
Group=pi
WorkingDirectory=/home/pi

Finally, you don't need ".service" in your systemctl commands. sudo systemctl enable nodered is sufficient

Better, I think, to run the script. It may also tweak the max_old_space_size and it will make sure that you have the latest versions of all the scripts.

im also facing the same issue, I had nodered pre installed with the os in raspberry pi. After updating it, its not running on startup. I even tried to run the script code which is given in this chat..

Raspberry Pies do not (usually) come with an OS installed.
RPiOS does not come with Node-red installed.

Therefore you will have to give us more information:
Is this a Pi and OS set up for a special role? Where did you get it from?
What model is the Pi?
What OS is installed? 32bit or 64 bit?
What username do you have?
What does ls -ld ~ return? (those are little Ls not 1s)
What does ls -al $HOME/.node-red return?
What script code did you try to run? How? What happened?
Are you willing, if necessary, to burn a new operating system image to a micro sd card?

I believe that for a while node red was installed in the base image.

rpi os 32 bit thats what i have installed in it(rpi 4).. it comes with node red
Actually what happened was it was running fine (node-red was fine , with auto start) and then i did the update sudo apt update and upgrade .. and after restarting rpi, node-red stopped running with auto start.
even if i try to run with "node-red-start" command, it throws error

Could not find user directory. Ensure $HOME is set for the current user, or use --userDir option
nodered.service: Main process exited, code=exited, status=1/FAILURE
nodered.service: Failed with result 'exit-code'.
nodered.service: Consumed 1.422s CPU time.

I am not able to run it in autostart mode as well.
username is pi
but when I checked in nodered.service, I could find that username and group is empty.
I tried to edit it and save it .But I couldnt save as it showed me permission based error.

What does ls -ld ~ return? (those are little Ls not 1s)
What does ls -al $HOME/.node-red return?
I cant run these 2 now as it is in remote area.
can you help me out.?

Running the Node-RED install script as the pi user (without sudo) and adding —node16 at the end of the command to force install of nodejs 16 should clean it up for you

Hello jbudd, thanks man..
I removed node js, node-red and reinstalled with the script.. now its running fine.

cause of the total mess : I did total update, so it also updated node red and it caused problem with inbuilt node red which came with default os(which was pre installed). This messed up whole configuration.
previously when I ran node red, i reached it using 169.xxx.xxx.xx:1880 and after update, when i started the node red using icon or code "node-red-start", it gave me the above mentioned error. It also prevented me from enabling autostart service. Instead of that, I was able to run it using "node-red" command in the terminal and I could reach it out using local host with port 1880.

can someone explain what happened in this and so that I can have a better 'right' understanding on this topic.
support will be appreciated.
Thankyou, once again thanks Jbudd

As you describe it, it appears to be related to the default install of node red. We don't know about that so it is difficult to say. I believe that, with the current versions of the pi os, node-red is no longer installed by default so the problem should not arise again. It is precisely to avoid these sort or problems that the recommended install script is provided, so that everybody using that starts from a known state.

At one time, Node-red was included with Raspbian but that ceased in 2018, and that OS would not run on a Pi 4.

Anyway, however NR got onto your Pi, I suspect it was an old version which got broken by apt upgrade moving to an incompatible version of node.js.

Incidentally, Node-red appears at the very top of the latest RPiOS changelog:

2022-09-22:

  • NodeRED removed from Recommended Software and full image - should only be installed via apt

It would be interesting to know the reason RPi Ltd seem ignorant of NR and it's correct installation.

1 Like

The Raspberry Pi guys are fully aware of where we are. The main issue is that they will only allow software to be installed as a deb file and pre-req other deb packages. The problem is that the official Debian nodejs deb package is way behind current LTS.

Until recently the full-install version of RaspiOS did include Node-RED. No idea where you got 2018 from.

However, as of now it means that we can only install Node-RED v2 as a deb, so rather than have that pre installed we decided to leave it in their repo if required, but no longer pre installed in the full install version nor to recommend use of the deb.

The other technical reason is that since Pi now make you choose a default id the full install can’t assume use of the pi user and thus where we store flows under, and trying to change all our docs to say “if you use this install your flows may not be where we’ve been telling you they are for 7 years” would be too disruptive just for the edge case of a fully installed pi, when most users use basic desktop or minimal headless install, and then have to manually install it and it then gets installed using the correct user.

1 Like

https://downloads.raspberrypi.org/raspios_full_armhf/release_notes.txt

2018-06-27:

  • Bluej, Greenfoot, NodeRED, Claws Mail, VNC Viewer removed from image - can now be installed from Recommended Applications