0

I have a node library with multiple scripts (ES modules to be precise). One of the module, when invoked, uses exec function from built-in child_process module. I need to run ts-node command using the exec function.

The problem is that ts-node is not recognized as exec doesn't do the path manipulation magic that npm scripts do by appending the node_modules/.bin to the path variable. Further, I cannot rely on __dirname and __filename special values as I use ESM and not CJS (I know there is a way to get these values but I would rather prefer if there is any standard approach to execute commands with the semantics of npm scripts).

So, how can I execute ts-node command from my own script which could be installed anywhere (as a nested dependency)?

Harshal Patil
  • 17,838
  • 14
  • 60
  • 126
  • Is `ts-node` installed globally or locally? You can easily create your own `__dirname` and `__filename` with the exact same behavior: https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules There is no standard way. – jabaa Feb 10 '23 at 10:06
  • The `ts-node` is installed locally. I am thinking will `npx ts-node` will do that job. But I am not able to find the official documentation to check if `cwd` is the starting point for `npx` lookup before fetching or looking for `ts-node` in local system cache. – Harshal Patil Feb 10 '23 at 10:17
  • `npx` traverses upwards until it finds a `package.json`. If it finds one, it checks there if it can find the required module. It's not `cwd`. – jabaa Feb 10 '23 at 10:23

0 Answers0