remy / nodemon

Monitor for any changes in your node.js application and automatically restart the server - perfect for development
http://nodemon.io/
MIT License
26.23k stars 1.72k forks source link

Defaulting to `ts-node` is confusing when using ESM loaders #2083

Closed LinusU closed 1 year ago

LinusU commented 1 year ago

Expected behaviour

I was expecting @esbuild-kit/esm-loader being used to load my TypeScript files, as that is the case when I run NODE_OPTIONS='--loader @esbuild-kit/esm-loader' node src/server.ts (same command but with node instead of nodemon).

Actual behaviour

It uses ts-node in order to transpile the TypeScript files.

I only noticed this because source maps wasn't working properly, and upon investigating that I realised that nodemon had actually used ts-node to transpile the typescript files.

Steps to reproduce

  1. Create the following package.json file
    {
      "type": "module",
      "dependencies": {
        "@esbuild-kit/esm-loader": "^2.5.0",
        "nodemon": "^2.0.20"
      }
    }
  2. Run npm install
  3. Create the following index.ts file:

    const test = (foo: string) => { throw new Error(foo) }
    
    test('foo')
  4. Run NODE_OPTIONS='--loader @esbuild-kit/esm-loader' node index.ts
  5. Observe nice stack trace
  6. Run NODE_OPTIONS='--loader @esbuild-kit/esm-loader' nodemon index.ts
  7. failed to start process, "ts-node" exec not found

remy commented 1 year ago

I'm not really familiar with TypeScript (mostly intentionally), but going by the steps you've included, it looks like a good work around would be to not use the ts-code thing if there's NODE_OPTIONS set.

Would that sound about right?

LinusU commented 1 year ago

NODE_OPTIONS can be used to pass any options that could also be given to node via passing cli arguments, so potentially it's used for other things where people want the ts-node behaviour. Maybe checking if --loader is present in NODE_OPTIONS is a better approach?

Personally, I would drop ts-node from the default execMap, since there is multiple programs that run .ts file (e.g. Deno). And just inside node there are multiple solutions other than ts-node (e.g. tsx).

I'd be happy to submit a PR for any of the above approaches ☺️

remy commented 1 year ago

I genuinely don't care about TS support, but it's been in nodemon for too long that removing it would probably break expectations.

I think checking the --loader is the best (soft) approach that could solve this issue.

LinusU commented 1 year ago

Sounds good πŸ‘

Where in the code do you think it would be best to add the check? In nodemon/lib/config/defaults.js?

I'm thinking something like (process.env.NODE_OPTIONS || '').includes('--loader')

github-actions[bot] commented 1 year ago

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

LinusU commented 1 year ago

ping @remy, do you have any thoughts on my last question?

github-actions[bot] commented 1 year ago

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

LinusU commented 1 year ago

friendly ping @remy

github-actions[bot] commented 1 year ago

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

LinusU commented 1 year ago

friendly ping @remy

github-actions[bot] commented 1 year ago

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

LinusU commented 1 year ago

friendly ping @remy

github-actions[bot] commented 1 year ago

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

LinusU commented 1 year ago

friendly ping @remy

Just need to know where you want the code and I'd be happy to send a PR πŸ˜…

github-actions[bot] commented 1 year ago

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

LinusU commented 1 year ago

friendly ping @remy

Just need to know where you want the code and I'd be happy to send a PR πŸ˜…

remy commented 1 year ago

Sorry, talk about having to prod me!

You had the solution a good while ago, but it was quicker for me to make the PR! I'll get it merged today.

LinusU commented 1 year ago

Hehe, no worries, thanks for fixing this πŸ™

Would love to see a release with this in it πŸš€

remy commented 1 year ago

I seem to have lost the PR I was planning to make. Good lord I'm distracted quickly!

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 2.0.21 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 2.0.22 :tada:

The release is available on:

Your semantic-release bot :package::rocket: