Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is node_modules directory in AngularJS?

Tags:

I am exploring AngularJS tutorial project and found it has node_modules directory inside, which size if 60 megabytes.

Does simple clientside javascript project really need so huge corpus of unknown data?

I tried to delete this directory and project still works. I suspect it somehow relates with node.js and it's npm but how? Suppose I need to run my project on some conventional web server (not node.js), then how to know, which files/directories are unneeded?

Many javascript libraries require to use bower to install them. If I use bower, does this mean I need to keep node_modules?

like image 483
Dims Avatar asked Dec 30 '15 09:12

Dims


People also ask

What is the node_modules directory?

A node_modules directory contains all the React dependencies packages: react , react-dom , and their transitive dependencies like webpack , babal , rxjs , ESLint , etc., to build and run a React project.

What is node_modules in node JS?

You can think of the node_modules folder like a cache for the external modules that your project depends upon. When you npm install them, they are downloaded from the web and copied into the node_modules folder and Node. js is trained to look for them there when you import them (without a specific path).

Where is node_modules folder?

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node. Non-global libraries are installed the node_modules sub folder in the folder you are currently in.

Can node_modules folder be deleted?

Using 'Git Bash' to remove the folder is the simplest way to remove all folders including subfolders in the 'node modules'. It will take a while to delete everything, but it works without any installation.


2 Answers

The node_modules directory is only for build tools.

The package.json file in the app root defines what libraries will be installed into node_modules when you run npm install.

Very often with an angular app, on your dev machine or on a build server, you use other Javascript libraries from npm (a node.js package manager) to build your angular app. Tasks can be concatenating resources, using CSS preprocessors like LESS or SASS, minification, replacing of values, etc. etc. The most common tools for managing and running these tasks are called grunt and gulp, which are installed through npm as well.

When you deploy your app, you only distribute the resulting build, not any of the source files or build tools.

It is of course possible to write an AngularJS app without building anything.

edit from comments: When you dive into Angular more, there are more advanced techniques of using libraries installed by npm even in the client app, you then selectively choose the ones you need, not the whole 50MB+ thing. I'd recommend staying with the basic approaches until you get a good grasp on them though.

like image 85
vvondra Avatar answered Sep 20 '22 15:09

vvondra


NPM is the node package manager, which installs packages locally into a project, specifically, into the node_modules folder. From there the package code can be included into a project, yes, can is the important word there.

The browser has no way to include modules in code (yet), so you need to use a library that can expose node's commonJS style modules. Browserify and Webpack are two popular methods of doing so.

Angular complicates this by introducing its own module system, which more closely resembles AMD-style modules. There are ways around this, so that you can use node-style modules, maybe your project uses those.

Using npm for managing dependencies is a great idea, its a fantastic package manager. It is likely in your case though that the project is only built using node and that the node_modules folder contains dependencies only related to the build.

like image 20
Matt Styles Avatar answered Sep 19 '22 15:09

Matt Styles