英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论