Skip to content

Conversation

jboucly
Copy link

@jboucly jboucly commented Oct 19, 2025

Description

This pull request aims to add new features for contribution as well as local use :

  • Add new script to run server npm run server:watch ➡️ This script run server only with live reload support for usage on development mode. Example for developers who develop modules and want to update them automatically when a change occurs 🚀

Test this pull-request

Hot reload

  • Run npm run server:watch
  • Make a change in any node helpers file of your modules and save it
  • Your server reload

@sdetweil
Copy link
Collaborator

You can reload the browser side anytime, ctl-r, or f5

it only loads the web side. You have to stop server and start to reload node_helpers

@sdetweil
Copy link
Collaborator

What does server dev do? dev was only for web side

if you open your own browser it doesn’t get that parm

@sdetweil
Copy link
Collaborator

The scripted installer handles node install plus other things

@jboucly
Copy link
Author

jboucly commented Oct 19, 2025

Yes, but stopping the server and then restarting it quickly becomes redundant when we make a lot of modifications and tests on the module side in development....

@jboucly
Copy link
Author

jboucly commented Oct 19, 2025

You can reload the browser side anytime, ctl-r, or f5

it only loads the web side. You have to stop server and start to reload node_helpers

Yes, but stopping the server and then restarting it quickly becomes redundant when we make a lot of modifications and tests on the module side in development....

What does server dev do? dev was only for web side

if you open your own browser it doesn’t get that parm

I don't understand

@sdetweil
Copy link
Collaborator

sdetweil commented Oct 19, 2025

So again, change module, ctrl-r if cursor is over web side, f5 if over dev window side

what does server:dev do?
The dev parm is only used when electron is the browser in full mode, npm run start:dev

in run server mode
You have to start your own browser. Chrome/firefox/surf/safari pointing at the MagicMirror address and port

dev mode curl-shift-I , toggles the dev window open or closed

@KristjanESPERANTO
Copy link
Collaborator

what does server:dev do?

To start MagicMirror in server mode + Nodemon keeps an eye on server-side files, and whenever a change is detected the server restarts automatically.

@sdetweil
Copy link
Collaborator

Ah

@jboucly jboucly changed the title Create .nvmrc file and create server:dev script Create server:dev script Oct 21, 2025
@jboucly jboucly changed the title Create server:dev script Create server:watch script Oct 21, 2025
@KristjanESPERANTO
Copy link
Collaborator

Thanks for the changes 🙂 I would like to test something relevant. Give me some time for the review.

Jboucly and others added 10 commits October 22, 2025 13:26
…g and port management

- Add port availability checks before restart to prevent race conditions
- Add error handler for spawn failures
- Extract hardcoded values to constants (RESTART_DELAY_MS, PORT_CHECK_*)
- Add getConfigFilePath() helper for cleaner config path resolution
- Watch js/ and serveronly/ directories in addition to modules/ and config/
- Watch all JS files (.js, .mjs, .cjs) instead of just node_helper.js
- Improve restart mechanism with isRestarting flag
- Better error messages for watcher and spawn failures
- Remove unused app.restart() method from app.js

These changes make the watcher more robust and easier to maintain.
@KristjanESPERANTO KristjanESPERANTO force-pushed the feat/add-server-auto-reload-and-nvm-config branch from 206421c to c2103f5 Compare October 22, 2025 12:54
@KristjanESPERANTO KristjanESPERANTO changed the title Create server:watch script feat(core): add server:watch script with automatic restart on file changes Oct 22, 2025
@KristjanESPERANTO
Copy link
Collaborator

KristjanESPERANTO commented Oct 22, 2025

I have re-based the branch.

During my tests, I encountered various issues. For example, the restart was triggered by files that my editor created temporarily when saving. Furthermore, the restart did not wait until the old instance released the port, so the new instance could not use it. I have adjusted this and other details in one commit.

Please feel free to give feedback on my changes.

From my point of view, everything looks good now 😃 However, I would wait for the opinion of at least one other contributor on the current status.

Edit: I removed the manual restart option since it would be another feature. One feature per PR is easier to handle.

@jboucly
Copy link
Author

jboucly commented Oct 22, 2025

I have re-based the branch.

During my tests, I encountered various issues. For example, the restart was triggered by files that my editor created temporarily when saving. Furthermore, the restart did not wait until the old instance released the port, so the new instance could not use it. I have adjusted this and other details in one commit.

Please feel free to give feedback on my changes.

From my point of view, everything looks good now 😃 However, I would wait for the opinion of at least one other contributor on the current status.

Edit: I removed the manual restart option since it would be another feature. One feature per PR is easier to handle.

Nice jobs ! Thanks for the changes ! I see that I don't have the whole history of the project when I see all the changes you made 🫣

Ok for the removal of manual reload and sorry for my english in changelog file x')

But i don't understand why on your local you have this bug with my version of watcher 🤔

Edit: I change the description of the PR

// We watch specific directories instead of the whole project root to avoid
// watching unnecessary files like node_modules (even though we filter it),
// tests, translations, css, fonts, vendor, etc.
watchDir(path.join(__dirname, "..", "modules"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KristjanESPERANTO

Nitpicking stuff from my side ...

AFAIR only modules/default is fix, the modules path for 3rd-party can be changed in config.js (see foreignModulesDir defined in defaults.js). There is an environment variable MM_MODULES_DIR too. You can search for modulesDir in the project to see the construction ...

Other point: You excluded css in the comment above, but what about custom.css? If this file should be watched: Same construction as for modules, customCss in defaults.js, env var MM_CUSTOMCSS_FILE ...

So better approach here would be (untested):

const { getEnvVarsAsObj } = require("#server_functions");
const env = getEnvVarsAsObj();
watchDir(path.join(__dirname, "..", env.modulesDir));
watchDir(path.join(__dirname, "..", env.customCss));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You excluded css in the comment above, but what about custom.css?

Hm... If the CSS changes and the watcher restarts the server, the frontend would not restart/reload. That's why I removed the CSS monitoring, as it would not make sense with the current logic.

AFAIR only modules/default is fix, the modules path for 3rd-party can be changed in config.js (see foreignModulesDir defined in defaults.js). ...

Good point. That should be manageable.


I just had an idea for how to create a common watcher that would work in both server and electron mode and can reload the browser/electron. I'll give it a try, but that needs some refactoring.

I think a watch mode also for electron would definitely be nice. Don't you think? I'll try to look into that over the next few days.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the frontend will restart after server restart (with a little delay), AFAIR this was implemented some times ago. I use this in my setup (Browser gets refreshed when docker container was restarted).

Watching/Restarting is also possible with pm2 ...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the frontend will restart after server restart

Okay, I'll test it. My previous tests were probably too short.

Watching/Restarting is also possible with pm2 ...

Or do you think a watch mode in the core is unnecessary? I find it kind of interesting to be able to do this without nodemon or pm2. But of course, we don't want the core to complex.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pm2 is already in this project (not happy with this because I remove this when building the container images) so was only meant as hint before reinventing the wheel ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hence the fact that I proposed Nodemon to have everything already built without reinventing the wheel.
Nodemon already handles these reloading aspects for CSS, js, etc.

What I'm afraid of is that the watcher.js will become complex... Nodemon can be implemented in devDependencies and therefore not installed during a Prod installation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants