Oscar Rene
Oscar Rene

Reputation: 361

Using same codebase for different NodeJS projects

Right now I got a NodeJS backend for a system I built. The problem is that I need to also maintain another instance of the backend for some client specific requirements, both of the instances share like 70-80% of the code. At the moment I'm using git branches to keep them apart, I know git is not meant to do this, so I would like to know if there is something that allows me to have two separate projects sharing some codebase, similar to flavors in Android.

Upvotes: 1

Views: 721

Answers (1)

BlackStork
BlackStork

Reputation: 8331

There are few options to do this:

1. Install your own module as separate dependency with npm via package.json dependency.

2. Use docker.

Docker is container virtualisation engine, that allows you to create images of vritual environments with pre-installed infrastructure/file system

You just crete image with some linux os inside, node and your module preinstalled, and all you need is to mount your unique code as "volume" to the container and thats it.

  • use nodejs offcial image - it have everything basic node.js env would need - to create own image. In the folder where you have /reusable_code folder and package.json create Dockerile file

Dockerfile:

 FROM node:6.9.2
 RUN mkdir app
 COPY ./reusable_code /app/reusable_code
 COPY ./package.json /app/package.json
 WORKDIR /app
 RUN npm install -g <your global modules> && npm install

now run docker build -t base-image-with-reusable-code .

It will create and tag the image as base-image-with-reusable-code

Now once you want to use it with any unique code you should do from the folder where the code is (this assuming all unique code use same package.json dependencies used in previous step - if not this will need extra step)

docker run -ti -v ./app.js:/app/app.js -v ./unique_code:/app/unique_code base-image-with-reusable-code node app.js

Of course names should be corrected, and if you have different project structure then the changes should reflect that.

3. Link the reusable code module folder via OS

Simply put, just ln -s /path/to/reusable/code ./resuable_code from your unique code project root folder, and then use it assuming it residing at root of every unique project you have linked it to.

4. Link the reusable code module folder via npm link

as suggested by @Paul :

node native way to do #3 is via npm link, which both sets up the symlink and makes reference to it in your package.json so that your code can treat it as an external module

Assuming you have reusable code folder in same folder where unique code folders are located:

Modified example of the npm link docs:

       cd ~/projects/unique_project1  # go into the dir of your main project
       npm link ../reusable_code     # link the dir of your dependency

Note : all solutions assume you have separate git project for your reusable code. Generally speaking , it's a good practice.

Upvotes: 1

Related Questions