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

Unable to enable logs when using nodemon as a module #2195

Open
brainthinks opened this issue Apr 22, 2024 · 5 comments
Open

Unable to enable logs when using nodemon as a module #2195

brainthinks opened this issue Apr 22, 2024 · 5 comments
Labels
remy working on it stale no activity for 2 weeks

Comments

@brainthinks
Copy link

brainthinks commented Apr 22, 2024

  • nodemon -v: 3.1.0
  • node -v: 20.10.0
  • Operating system/terminal environment: Linux Mint (Ubuntu) / terminator
  • Using Docker? What image: Yes, node:20.10.0-alpine3.19
  • Command you ran: N/A (using nodemon as a module)

I am using nodemon as a module, according to this documentation - https://github.com/remy/nodemon/blob/main/doc/requireable.md

The documentation does in fact work, however, the logging that I'm used to seeing when running nodemon was noticeably absent. I even imported the nodemon utils file and called utils.log.info, but it produced no log. After longer than I care to admit, I figured out that the culprit here was the isRequired getter on the nodemon utils.

Expected behaviour

I can enable / disable the logger, so that I can see the normal nodemon logs rather than needing to reimplement them myself with a different logging solution.

Actual behaviour

If I require the module, I have no built-in option for getting the normal logging working.

Steps to reproduce

  1. create a new .js file (I created a .cjs file because I'm using ESM, e.g. { "type": "module" } in my package.json, I will test this with ESM later
  2. make the file executable e.g. chmod +x path/to/script.js
  3. copy the contents of my script into that .js file (see code block below)
  4. run the script, e.g. ./path/to/script.js
  5. observe that no normal nodemon logging is visible in the terminal e.g. no colored logs that begin with [nodemon]

Here is the script I used:

#!/usr/bin/env node

/**
 * @see https://github.com/remy/nodemon?tab=readme-ov-file#using-nodemon-as-a-module
 * @see https://github.com/remy/nodemon/blob/main/doc/requireable.md
 */

const utils = require('nodemon/lib/utils/');
const nodemon = require('nodemon');

// @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/nodemon/index.d.ts
const config = {
  verbose: true,
  watch: [ './dist' ],
  delay: 2000,
  exec: 'yarn run serve:dev',
  // @see https://github.com/remy/nodemon?tab=readme-ov-file#application-isnt-restarting
  legacyWatch: true,
}

nodemon(config);

nodemon
  .on('start', function () {
    console.log('test -----------------------------')
    console.log(utils.log.info);
    console.log(utils.log.info.toString());
    utils.log.info('App has started');
  })
  .on('quit', function () {
    utils.log.info('App has quit');
    process.exit();
  })
  .on('restart', function (files) {
    console.log('test 2 -----------------------------')
    utils.log.info('App restarted due to: ', files);
  });

P.S. I suspect that this issue from a decade ago may have been related - #301

EDIT: I was able to get this working with ESM. here it is if anyone is interested:

import utils from 'nodemon/lib/utils/index.js';
import nodemonLogger from 'nodemon/lib/utils/log.js';
import nodemon from 'nodemon';

// @see https://github.com/remy/nodemon/issues/2195
utils.log = nodemonLogger(false);

// @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/nodemon/index.d.ts
const config = {
  verbose: true,
  watch: [ './dist' ],
  delay: 2000,
  exec: 'yarn run serve:dev',
  // @see https://github.com/remy/nodemon?tab=readme-ov-file#application-isnt-restarting
  legacyWatch: true,
}

nodemon(config);
@brainthinks
Copy link
Author

FWIW, I was able to work around this issue by exploiting utils, but surely this isn't a recommended approach:

#!/usr/bin/env node

/**
 * @see https://github.com/remy/nodemon?tab=readme-ov-file#using-nodemon-as-a-module
 * @see https://github.com/remy/nodemon/blob/main/doc/requireable.md
 */

const utils = require('nodemon/lib/utils/');
const nodemonLogger = require('nodemon/lib/utils/log'); // <-- import the logger so we can create a custom instance
const nodemon = require('nodemon');

utils.log = nodemonLogger(false); // <-- overwrite the logger with a custom instance, where we manually provide the value for `utils.isRequired`

// @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/nodemon/index.d.ts
const config = {
  verbose: true,
  watch: [ './dist' ],
  delay: 2000,
  exec: 'yarn run serve:dev',
  // @see https://github.com/remy/nodemon?tab=readme-ov-file#application-isnt-restarting
  legacyWatch: true,
}

nodemon(config);

nodemon
  .on('start', function () {
    console.log('test -----------------------------')
    console.log(utils.log.info);
    console.log(utils.log.info.toString());
    utils.log.info('App has started');
  })
  .on('quit', function () {
    utils.log.info('App has quit');
    process.exit();
  })
  .on('restart', function (files) {
    console.log('test 2 -----------------------------')
    utils.log.info('App restarted due to: ', files);
  });

@brainthinks
Copy link
Author

@remy I assume that there is a good reason for nodemon to be able to know whether or not it's being used as a module. Therefore, if you think it would be a reasonable approach to have a brand new configuration option for enabling built-in logging despite being required as a module, I'm happy to submit a PR for it.

@remy
Copy link
Owner

remy commented Apr 23, 2024

Let me take a look at the code first to check how you're using and how the require mode works then I'll get back to you.

Copy link

github-actions bot commented May 7, 2024

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

@github-actions github-actions bot added the stale no activity for 2 weeks label May 7, 2024
@remy
Copy link
Owner

remy commented May 19, 2024

Hey, sorry for the delay, I don't often get time to dedicate to sitting down and debugging nodemon - but I am now.

I'm not 100% sure what you need, but I'm pretty sure it's available.

The reason that nodemon wants to know if it's required or not is because when it's spawned as stand alone, it will print to stdout. When it's required, it should be quiet by default. It's also things like (quite often) if nodemon is not required, then it will exit with process.exit(n) for help or take control of STDIN, etc.

I can see (now) that none of my examples actually show you how to get the logging (in my docs), but it is included in the message types.

You need to hook the log event which accepts an event argument as such:

type NodemonEventLog = {
  /**
    detail*: what you get with nodemon --verbose.
    status: subprocess starting, restarting.
    fail: is the subprocess crashing.
    error: is a nodemon system error.
  */
  type: 'detail' | 'log' | 'status' | 'error' | 'fail';
  /** the plain text message */
  message: String;
  /** contains the terminal escape codes to add colour, plus the "[nodemon]" prefix */
  colour: String;
};

Although I've been writing JavaScript for a few decades, my TypeScript is … not the favourite thing I do, but I've started a PR that adds types to help developers, like yourself, trying to require the module: #2204

Hopefully the log type is what you needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
remy working on it stale no activity for 2 weeks
Projects
None yet
Development

No branches or pull requests

2 participants