Home > database >  Is it possible to record the file execution order in a node / express project?
Is it possible to record the file execution order in a node / express project?

Time:01-09

I'd like to see which file is executed before which file when running the program. and maybe log each file name in the console like this:

npm run dev ...

-> 1. src/index.ts

-> 2. src/model/something.ts

-> 3. src/lib/something.ts

-> ...

I could add a log function to every file, but I need to check many files.

CodePudding user response:

You can use a package like https://www.npmjs.com/package/require-in-the-middle to add a callback handler that gets called whenever require is called (require is the function Node.js uses to load files), and you can log the file that gets require'd in this callback function.

If you're using ESM, you'd have to create a custom ESM loader hook (https://nodejs.dev/en/api/v18/esm/#resolvespecifier-context-nextresolve)

  • Related