I am currently working on a project at a large company, and according to the project I am working on, every time I want to quickstart the app, I would need to first run the command npm install
and then run all the additional compiling instructions, but the problem is that running npm install
can take a long time, and that is why I am wondering if it is necessary to run this command every time I make a change to the code, and then want to compile and run it.
What exactly does npm install
do? If you could explain to me in terms of how we compile and run java code i.e. javac bob.java
&& java bob
and try to make analogies on that basis, that would greatly help me understand the concept. The way I am currently thinking about it right now is that npm install
kind of runs like how javac
runs, but I am not sure if that is correct. Thanks in advance!
npm install downloads a package and it's dependencies. npm install can be run with or without arguments. When run without arguments, npm install downloads dependencies defined in a package. json file and generates a node_modules folder with the installed modules.
We do not need to install a module every time when installed globally. It takes less memory as only one copy is installed. We can make . js scripts and run them anywhere without having a node_modules folder in the same directory when packages are installed globally.
Use npm install to install new dependencies , or to update existing dependencies (e.g. going from version 1 to version 2). Use npm ci when running in continuous integration, or if you want to install dependencies without modifying the package-lock. json .
js and npm. To publish and install packages to and from the public npm registry or a private npm registry, you must install Node. js and the npm command line interface using either a Node version manager or a Node installer.
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/
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With