简体   繁体   中英

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.

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.

To prevent the modules from being added to your git repository (and therefore to github) you can add the node_modules folder to your .gitignore file. (As @Kato just pointed out)

However, you might consider keeping the modules in your repository to make sure everybody has the exact same version. Using npm install could lead to people getting different (more recent) version of a module.

This is a good blog post on the subject: http://www.futurealoof.com/posts/nodemodules-in-git.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM