Visitar URL original
Watch mode: Allow adding files to watch when using the --watch flag · Issue #45467 · nodejs/node · GitHub
The Wayback Machine - https://web.archive.org/web/20221115224831/https://github.com/nodejs/node/issues/45467
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch mode: Allow adding files to watch when using the --watch flag #45467

Open
gustavnikolaj opened this issue Nov 15, 2022 · 3 comments
Open
Labels
feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors. watch-mode Issues and PRs related to watch mode

Comments

@gustavnikolaj
Copy link

gustavnikolaj commented Nov 15, 2022

What is the problem this feature will solve?

The new --watch flag will automatically restart the process when any imported file is changed.

The new related --watch-path flag will restart the process when one of the added paths change.

Using --watch-path disables the automatic restarting based on imported modules.

--watch is what you want 99% of the time, but there are cases where you want to extend the watched files ever-so-slightly.

One such example is .env when used with the dotenv module. No matter if you do it programmatically, or through the use of a -r flag to the node process, changes in the .env. file will not trigger a reload.

Other examples could be configuration files in json, yaml og toml formats.

What is the feature you are proposing to solve the problem?

A new flag which will allow extending the set of watched files based on what is imported/required, with specific non-js files which are loaded through non-discoverable methods, without disabling the automatic discovery of files.

The automatic discoverability of files to watch is a killer feature! Unfortunately not being able to add individual files to the watched set is preventing me from using it.

What alternatives have you considered?

I can keep using nodemon or the current --watch-path feature, but have to enumerate and eagerly watch all files that could possibly be loaded. But as --watch-path does not work on Linux it will likely have to be nodemon.

@gustavnikolaj gustavnikolaj added the feature request Issues that request new features to be added to Node.js. label Nov 15, 2022
@gustavnikolaj
Copy link
Author

gustavnikolaj commented Nov 15, 2022

cc @MoLow

(I would add the watch-mode label if I had permission to do so)

@cola119 cola119 added the watch-mode Issues and PRs related to watch mode label Nov 15, 2022
@aduh95
Copy link
Contributor

aduh95 commented Nov 15, 2022

Could you illustrate your request with a minimal example? IIUC for the following setup:

echo '{"key": "initial value"}' > file.json
echo 'module.exports = JSON.parse(require("node:fs").readFileSync("./file.json", "utf-8"))' > dep.js
echo 'console.log(require("./dep.js"))' > entry.js
node --watch-path=file.json --watch entry.js

You would like that on top of any change to file.json, any change to dep.js would also restart the process, is that correct? Or are you talking about having a new API, and if so, can you give an example how it could look like?

@gustavnikolaj
Copy link
Author

gustavnikolaj commented Nov 15, 2022

You would like that on top of any change to file.json, any change to dep.js would also restart the process, is that correct?

That illustrates the behavior I'm looking for, correctly!

Some javascript code loads a non-javascript file and use it as input. In my example it is the dotenv package, which when used as node --require dotenv/config ... will load the .env file in the current working directory and populate process.env with the values there. I want changes in the .env file to cause a reload, and I am alright that I have to manually tell node to do so, when it cannot trivially be statically analyzed.

Or are you talking about having a new API, and if so, can you give an example how it could look like?

Honestly, I don't really know. The default behavior of --watch is brilliant. This is how nodemon or all other watch tool should work by default, and why such a tool belongs in core. I was a bit surprised to see that --watch-path implicitly de-activate the behavior of --watch, but I assume that there is a good reason.

--watch-path-but-also-do-not-disable-normal-watch is my naive request, but I am pretty sure that there is a better way. And I'm also pretty sure that I am not the only one with this use case, so I thought I would add the feedback here when I did not find another issue addressing it.

@MoLow MoLow added the good first issue Issues that are suitable for first-time contributors. label Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors. watch-mode Issues and PRs related to watch mode
Projects
None yet
Development

No branches or pull requests

4 participants