Code Sharing between Microservices

Code Sharing Option Chosen

This is documenting code sharing by the method of publishing a custom NPM package. To do this I will take the code that should be shared, put it in its own folder and publish it to the NPM registry.

Advantages of this

  • Services can use different versions of the common code if needed.
  • You can secure your common library by either creating a private organization in the NPM Public Registry or you can create a NPM Private Registry.

Overview of steps

  1. Create organization in the NPM Registry
  2. Publish NPM Modules to your NPM Registry organization
  3. Setup Project to Share Modules with Other Services
  4. Install your package into your other services

Steps

Create an Organization in the NPM Registry

There are several ways to publish the common code: Publish to the NPM public register, putting the lib in an org within the registry (either public or private), or creating a private NPM Registry. Go here for steps on creating an organization in the NPM registry.

Publish NPM Modules

Here I will create my shared common library and try to publish it to the organization I created

  • Create the directory
  • Change into the directory
  • Generate package.json file
 npm init -y
  • Open the package.json file
    • Change the name to your @accountName/directory
  • Inside the directory create git repository
git init

git add .

git commit -m "init commit"
  • Then publish to NPM
npm publish --access public
You may need to run npm login before the publish if getting an error

Setup Project to Share Modules with Other Services

Write the common library in typescript but compile it down to Javascript before publishing because there might be differences in typescript settings between the different services and common library as well as some services may not use Typescript at all.

  • In the library directory generate typescript config file
tsc --init
  • Install typescript and del-cli for development. del-cli package is useful for cleaning out old directories. In this case it will help delete the build directory
npm install typescript del-cli --save-dev
  • Create folder src in the library folder and an index typescript file ex. /common/src/index.ts
  • Update the package.json file to run the typescript compiler
    • in scripts create a build command of tsc
{
  "scripts": {
    "clean": "del ./build/*",
    "build": "npm run clean && tsc"
  },
}
  • Also update the main element in the package.json to tell npm what file we are wanting to import when we attempt to import the module so make the following changes to main element. The types setting will then tell TS what the main type definition file is. The files setting will tell NPM which set of files we want to be included in the final published version.
{
  "main": "./build/index.js",
  "types": "./build/index.d.ts",
  "files" : "build/**/*"
}
  • Then we need to setup the typescript compiler where to find source code and where to place it. Modify the tsconfig.json
    • uncomment declaration: true which will make sure when typescript is translated to javascript it will also generate a type definition file which will tell typescript whats going on in the module
    • uncomment outDir and change the path to a build folder which will house the compiled files
{
  "compilerOptions": {
    "declaration": true, 
    "outDir": "./build"
  }
}

  • Create GIT ignore to exclude build and node modules directories from GIT
node_modules
build
  • Now you can run npm run build which will delete the build directory, take the typescript file, convert it to javascript, and write it to the build directory that we just declared in the tsconfig file.

Change, Build, Publish

Process to make a change to the lib, build it and publish it up to NPM

git add .

git commit -m "made a change"

Increment package with npm version patch 

npm run build  

npm publish