Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js + Git: How To Avoid Adding Module Dependencies to Repository

My Situation

I'm developing a Node.js application as npm module.

In order to be able to run this app, I installed all the npm dependencies. So, my project contains some ./node_module/ folders containing external module dependency builds.

I maintain my project using git and I publish using a GitHub repository.

My Problem

I don't want to publish all my local builds of the npm module dependencies on GitHub.

I only want to publish my own code. The users of my module should use npm install to get the current builds dependencies.

My Question

What's the best way to keep the dependency builts away from my GitHub repository?

Thanks for your answer (or comment). - If anything's unclear, please comment.

like image 436
fdj815 Avatar asked Jan 28 '13 15:01

fdj815


People also ask

Should you git ignore node_modules?

Check node_modules into git for things you deploy, such as websites and apps. Do not check node_modules into git for libraries and modules intended to be reused. Use npm to manage dependencies in your dev environment, but not in your deployment scripts.

Do I need to push node_modules?

Never push node_modules. So now that you understand that your dependencies are system specific and might include native modules, you should never assume that your node_modules folder will work in production.

Why is committing node_modules bad?

5, Any particular downside of committing node_modules? The downside is that you'll have a hard time scaling your app horizontally this way also it may happen that some dependencies change in the mean time. Well in my case it is less likely that I'll need to scale it horizontally in next 6–10 months.


1 Answers

In the root directory of your project (the one above node_modules) add the following line to the end of your .gitignore file (create it if needed):

node_modules

That will prevent the node_modules files from being added to the GitHub repository.

Then create or open the package.json file in that same directory, and add a dependencies section like so:

{
   "dependencies": {
      "module": "0.0.x",
      ...
   }
}

Generally, you'll want to use 0.0.x for the format of your versions. This ensures you get bug fixes but no breaking or major changes in your dependencies, so they will stay compatible with your module.

The package.json will tell npm to install anything listed in dependencies whenever your module gets installed. You can read more about package.json here.

And here is a great little overview of the entire process.

like image 146
Kato Avatar answered Sep 28 '22 13:09

Kato