Reputation: 13
I have a Node.js application which can work even if I completely remove its source files from the computer.
How is it possible?
I heard a little bit about that Node.js app is automatically caching source files to work more efficiently.
Does it mean that these files are still existing somewhere?
Upvotes: 1
Views: 1080
Reputation: 2642
Despite JavaScript being an interpreted language, node.js compiles your source files into native machine code. Once you start an application, you can delete the JavaScript files and it'll continue to run as the code has already been compiled.
Here's an excerpt from Wikipedia regarding node's v8 engine that may help you better understand what's going on:
V8 compiles JavaScript to native machine code (IA-32, x86-64, ARM, PowerPC, IBM s390 or MIPS ISAs) before executing it, instead of more traditional techniques such as interpreting bytecode or compiling the whole program to machine code and executing it from a filesystem. The compiled code is additionally optimized (and re-optimized) dynamically at runtime, based on heuristics of the code's execution profile. Optimization techniques used include inlining, elision of expensive runtime properties, and inline caching, among many others.
As stated by David784:
Might be worth noting that this compiling happens on an as-needed basis. For example, if you have a single require('./other.js') inside of a callback, other.js won't be interpreted/compiled until that callback is called.
So your application may work once deleted for specific operations, but once a new dependency is used, node won't be able to find your application files and will inevitably crash.
Upvotes: 1