在一个Node/Express项目中记录文件执行顺序是否可能?

huangapple go评论92阅读模式
英文:

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

问题

我想查看在运行程序时先执行哪个文件,然后再执行哪个文件,也许在控制台记录每个文件的名称,就像这样:

npm run dev ...

-> 1. src/index.ts

-> 2. src/model/something.ts

-> 3. src/lib/something.ts

-> ...

我可以为每个文件添加一个日志函数,但我需要检查很多文件。
英文:

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.

答案1

得分: 1

你可以使用像 https://www.npmjs.com/package/require-in-the-middle 这样的包来添加一个回调处理程序,每当调用 require 时都会调用它(require 是 Node.js 用于加载文件的函数),你可以在这个回调函数中记录被 require 的文件。

如果你正在使用 ESM,你需要创建一个自定义的 ESM 加载器挂钩(https://nodejs.dev/en/api/v18/esm/#resolvespecifier-context-nextresolve)。

英文:

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)

huangapple
  • 本文由 发表于 2023年1月6日 23:36:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75032982.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定