Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to run the command npm install every time I want to compile my project?

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!

like image 281
Mohit Jain Avatar asked Jul 24 '17 23:07

Mohit Jain


People also ask

Why do I need to run npm install?

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.

Do I need to install node modules every time?

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.

When should I run npm install?

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 .

Do I need to install npm after installing node?

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.


2 Answers

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!)

like image 115
viraj_os Avatar answered Oct 21 '22 03:10

viraj_os


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.

like image 37
Aurelien Gekiere Avatar answered Oct 21 '22 05:10

Aurelien Gekiere