1

I'm using Node.js CLI to test part of my app, but in some of those tests its loading modules that it doesn't need to load.

If I'm calling the program with node -r esm and using import statements to import the modules, is it possible to do some form of lazy loading so that I don't need to wait so long for Node.js to resolve unnecessary imports?

Most of my code does not use require to load modules.

libby
  • 585
  • 4
  • 15
  • How can it decide whether it needs them or not? – Bergi Jan 13 '22 at 02:29
  • Something like this but for `import` https://stackoverflow.com/a/37083469/3587229 – libby Jan 13 '22 at 19:13
  • No, I meant to ask: when does your code want to load a module? In what cases is it unnecessary? What is your application doing concretely? – Bergi Jan 13 '22 at 20:20
  • I am running CLI tests, most of which don't run `jimp` or `image-js` libraries, but some of which do. Basically, I wrote a script which basically makes API calls to my app, but my understanding is `import`'s DFS will just load all my libraries automatically and it sometimes takes way too long to run a simple test, especially when all my dev servers are up. It should only load a module when it calls the module's API. – libby Jan 14 '22 at 06:38
  • So "*my app*" is a http api which does some things with an image library, and you have a separate cli script that makes http requests and checks the responses? Or are you saying that your cli script spawns a new api (`node` process) for each test? – Bergi Jan 14 '22 at 06:52
  • No, my app is locally hosted javascript files that makes no http requests. It just runs code locally. – libby Jan 14 '22 at 18:40
  • So your app is a cli application? And your test runner spawns multiple `node` processes, once for each test? – Bergi Jan 14 '22 at 18:51
  • Can you post the code that is using that library, as well as how you import and call that code from your program entrypoint, please? – Bergi Jan 14 '22 at 18:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241071/discussion-between-libby-and-bergi). – libby Jan 14 '22 at 18:56

0 Answers0