Do I need to run the command npm install every time I want to compile my project?
NPM Install
npm install
simply reads your package.json
file, fetches the packages listed there from (usually) https://www.npmjs.com/
, and sometimes engages in the build steps for those packages.
So you only have to run npm install
when you change your package.json
file, and need to fetch new dependencies.
Keep in mind that npm install --save <packagename>
(or npm install -S <packagename>
) will update your package.json
and run npm install
all in one line!
You can view the results of your npm install
inside ./node_modules/
.
To compare to java
This might be a helpful resource if you're trying to get stuff done: Getting Started with Node.js for the Java Developer
Javascript is not a compiled language, unlike java. When you invoke javac
, the java compiler reads in all your .java
files, compiles them to java bytecode, and then writes them to .class
files, which can then be bundled together into a .jar
for execution.
Javascript doesn't do any of this! When you invoke node foo.js
, the node
executable wakes up, reads foo.js
, and gets to work invoking it line by line**. Node does other cool things, including maintaining an event loop (which allows it to operate "asynchronously", and allows it to be very efficient as a webserver-- it doesn't sit around waiting for requests to complete, it carries forward with the next event in the queue.
Node also performs JIT and optimization, these details allow it to improve the performance of sections code it notices are running "hot".
Note also that node.js
uses the V8 javascript engine (also used in Google Chrome). Really everything I've said above is handled by V8.
(** Technically there is a syntax checker which is run first, before execution. But this is not a compile step!)
It is not necessary to do "npm install" each time you want to compile. You just need to do it when you change the dependencies of your project.