showing results for - "node import inside function"
Neil
11 Sep 2016
1require() is on-demand loading. Once a module has been 
2loaded it won't be reloaded if the require() call is
3run again. By putting it inside a function instead of
4your top level module code, you can delay its loading
5or potentially avoid it if you never actually invoke
6that function. However, require() is synchronous and
7loads the module from disk so best practice is to load
8any modules you need at application start before your 
9application starts serving requests which then ensures 
10that only asynchronous IO happens while your
11application is operational.
12
13Node is single threaded so the memory footprint 
14of loading a module is not per-connection, 
15  it's per-process. Loading a module is a
16one-off to get it into memory.
17
18Just stick with the convention here and 
19require the modules you need at the top 
20level scope of your app before you start
21processing requests. I think this is a 
22case of, if you have to ask whether you need to write
23your code in an unusual way, you don't need to write your
24code in an unusual way.
25
26