Is there a way to track the duration of file imports in a Node.js application?
We have a TypeScript application running in VSCode, with the target set to ES2020 and the module format set to ESNext.
The application is launched using the following command:
{
"name": "Run",
"request": "launch",
"type": "node",
"args": [
"--loader",
"ts-node/esm",
"--experimental-specifier-resolution=node",
"--no-warnings=ExperimentalWarning",
"--env-file=.env",
"--env-file=.env-testnet-debug",
"build/app.js",
],
"cwd": "${workspaceRoot}",
"internalConsoleOptions": "openOnSessionStart"
},
However, when launching our application during development, there is a 5-10 second delay between pressing F5 in VSCode and seeing the first console.log output from app.ts/app.js.
The app.ts file contains standard imports for the rest of the application, such as:
import { App } from "./app/classApp";
...
we tried to re-write imports to dynamic way:
const { App } = await import("./app/classApp");
Then, the app starts immediately, and we can see that the classApp import is responsible for the 5-10 second delay.
We attempted to write a tool that automatically rewrites all imports to use await import() while collecting debug statistics. However, we encountered issues with TypeScript types when importing dynamically.
Is there an elegant solution to visualize the entire import tree, including both in-house and external libraries, so we can identify what is slowing down our app startup? Our goal is to mark certain (likely external) dependencies as dynamic imports, so they load during runtime instead of at startup.
Thanks for any advice
- we tried to rewrite imports to dynamic await imports
- we tried to search here on SO as same as ChatGPT to recommended solution but without success
- there is no bundler used, just tsc for transpilation typescript to JS and than NodeJS is used to execute these .js files