46

Most of the time, when I look at projects in github , I see lib/ directory.

I just googled if there is a conventional meaning of lib/ directory. But I couldn't find much.

I would like to know what lib/ directory is for just to choose where to start looking at in github projects.

Project which have lib/ directories.

orchestrator

gulp

FurkanO
  • 7,100
  • 5
  • 25
  • 36
  • 2
    You got an example? I know various applications and projects having a lib directory, but the meaning is different for most of them. – GolezTrol May 13 '16 at 22:18
  • I added an example I was just trying to understand. – FurkanO May 13 '16 at 22:19
  • Oo, I thought maybe it has a general meaning. – FurkanO May 13 '16 at 22:21
  • 1
    Not very general, but in your case it seems you look at a lot of JS code. See [this](http://stackoverflow.com/questions/5178334/folder-structure-for-a-node-js-project). It seems to be very popular in node.js and co. – sascha May 13 '16 at 22:21
  • 7
    `lib` is short for `library` which is often used for common files, utility classes, imported dependencies, or 'back in the days' also for dlls for (desktop) applications. It's in general a 'library' of supporting code for the core application. But there is no rule (not that I know of, at least) that says you should have a lib, or that its name is 'lib', or what to put in it. – GolezTrol May 13 '16 at 22:23

1 Answers1

45

The lib directory, for most of my projects, is where I place shared components that might be used in multiple facets of my application. Things in this folder should not be tightly coupled to your application, and should theoretically be able to be plucked from one project to another, and work as expected (assuming all dependencies are available). Some examples of things I put in my lib folders:

  • If I'm defining a policy/process for loading bootstrapped data off the page into my frontend code, I would define the class/method in a file in the lib folder. It is just functional, and not bound to any of my application code or DOM structure.
  • If I need to build my own UI components (let's say I need to merge some Tagging functionality into a text area, and am unhappy with existing OSS alternatives), I would do it here. The code in lib just outlines how my component works, but doesn't implement any application-specific functionality. I can then include this in my core application and use it as if it were a third party library.

Essentially, anything that could be a third party dependency that you don't want to place in it's own repository and set up management for, you can use lib and have that functionality cleanly decoupled within your primary app.

Mike Trpcic
  • 25,305
  • 8
  • 78
  • 114
  • 1
    Quick note that for actual third-party dependencies, I use the package manager prescribed location (e.g. `node_modules`) when available, or a `vendor` directory. – Mike Trpcic May 13 '16 at 22:30
  • would you hence add lib into `.gitignore`? – João Pimentel Ferreira Aug 28 '22 at 16:38
  • 1
    No, because your application requires that code to operate, and it can't be installed by any other means (i.e. it's not provided by a vendor or package manager). If you were to clone the repository on a new computer, you need to also pull the contents of `lib` for it to run. – Mike Trpcic Aug 28 '22 at 22:04
  • if I have in `lib` third parties modules, normally I fetch them during build – João Pimentel Ferreira Aug 28 '22 at 23:12
  • 3
    `lib` is not typically for storing third party dependencies, those are usually handled by a package manager (e.g. `npm` installing dependencies into `node_modules`), and if you are manually pulling third party dependencies, standard practice is to put them in a `vendor` directory. `lib` is typically for code that is written as a part of your application, but is not tightly coupled to the business logic of your application directly. – Mike Trpcic Aug 31 '22 at 00:41
  • npm has the default node_modules directory for third party dependencies, but they are to be used by the backend. I meant in the frontend upon building the dist directory. Where shall I put third party libs if I want to store these files in my server? – João Pimentel Ferreira Aug 31 '22 at 16:25