DEV Community

Garry Xiao
Garry Xiao

Posted on • Updated on

Build a React components NPM package and CI/CD with Github Action

Lockdown in NZ creates spare time for me to enjoy something occupied before with chore. Recently, I started to summarize the React framework of SmartERP that a SaaS service I led before, try some new solutions. I would like to demonstrate how to build an NPM (Node.js Package Manager) package and implement CI/CD with Github Action to automate most of the work.


  1. Create a new public repository "ETSOO/etsoo-react" on GitHub.
  2. Change to the target folder, clone the repository into it: "git clone".
  3. Install the latest Node.js to your computer(
  4. Run 'Node.js command prompt', input command 'npm init' to initialize an NPM package. 'npm init -y' will not ask any question and produce the package.json file with default values. 'npm init --scope=<your_org_name>' to create an Org scoped package.
  5. 'git add package.json', multiple files are separated by a blank (Boring? learn something from, 'git add -A'), then 'git commit -m "Message about the commit"' make changes to the local depository, then 'git push origin master' to upload changes to Github. If you made changes on Github, you need to first pull the updates 'git pull origin master' and then push the local version. Beware of any conflicts here. Run 'git stash git pull git stash pop' to keep local updates.
  6. Create an account in the NPM registry ( Enter the command 'npm login', provide account details you just have to complete it. Enter the command 'npm publish' to publish, every time needs to upgrade the "version" under package.json.
  7. IDE(Integrated Development Environment): Visual Studio Code,

React & TypeScript:

  1. 'npm install -g typescript react react-dom @types/react @types/react-dom' install the minimal set of dependencies required to React and TypeScript.
  2. Create a folder 'src', create an 'index.ts' under it. Change package.json, set "main": "lib/index.js" (It's not so perfect to include the ts files directly, with "src/index.ts" will cause parse error, seems tsc will not compile files under node_modules); "types": "lib/index.d.ts", scripts add "build": "tsc".
  3. Copy a 'tsconfig.json' from another project to the root and change settings as you want or 'npx tsc --init'. Set "declaration": true, generates corresponding definitions in index.d.ts. "jsx": "react" if include 'tsx' files. "outDir": "./lib" tell the compiler that the 'src' folder will be compiled to javascript in 'lib' folder. Add the folder name to '.gitignore'. Make sure "noEmit": false.
  4. If unknown errors occurred, run 'npm install' to check the dependencies and install any missing packages.
  5. Test your new NPM module without publishing it ( Run 'npm link' to define a global link. Then go to the project share this package, run 'npm link etsoo-react' to add the global link to the 'node_modules' like it has been published. Run '' to remove the link. Run 'npm unlink --no-save ' on your project’s directory to remove the local symlink, and run 'npm unlink' on the module’s directory to remove the global symlink. (26/05/2021 update: just install local package with: npm install file:../packagename and the link will be created automatically)


  1. Run 'npm i jest @types/jest ts-jest -D' to install the testing framework Jest ( Add a folder 'tests' under the root, add a test script file to pass the 'npm test' command.
  2. Add "jest": "^25.3.0", under package.json/devDependencies if not exits and run 'npm install'.
  3. add "jest": { "testMatch": [ "/tests/*/.js" ] }, to the package.json. Limit Jtest to the folder "test" under root.
  4. Install vscode-jest
  5. npm install -D react-test-renderer
  6. npm install -D babel-jest babel-core@^7.0.0-bridge.0 @babel /core @babel /preset-env regenerator-runtime (
  7. npm install --save-dev enzyme enzyme-adapter-react-16 @types/enzyme @types/enzyme-adapter-react-16


  "compilerOptions": {
    "outDir": "./lib",
    "target": "ES2018",
    "lib": [
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": false,
    "jsx": "react",
    "declaration": true
  "include": [
Enter fullscreen mode Exit fullscreen mode


  "name": "etsoo-react",
  "version": "1.0.2",
  "description": "ETSOO React TypeScript components NPM package",
  "main": "lib/index.js",
  "types": "lib/index.d.ts",
  "scripts": {
    "build": "tsc",
    "test": "jest"
  "jest": {
    "testMatch": [ "<rootDir>/tests/**/*.js" ]
  "repository": {
    "type": "git",
    "url": "git+"
  "author": "Garry Xiao",
  "license": "MIT",
  "bugs": {
    "url": ""
  "homepage": "",
  "devDependencies": {
    "@types/react": "^16.9.33",
    "@types/react-dom": "^16.9.6",
    "jest": "^25.3.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "typescript": "^3.8.3"
Enter fullscreen mode Exit fullscreen mode

Github Actions:

  1. Choose 'Publish Node.js Package' from 'Popular continuous integration workflows' under the Actions tab and create the template YAML file under 'etsoo-react/.github/workflows/'. View for help.
  2. Log in NPM, under 'Auth Tokens', create a new one and copy the token, then go to Github repository's settings, secrets, 'add a new secret' named 'GXNpmToken' here.
  3. Github desktop ( is an interesting tool to help you manage depositories.

YAML file content, please view After you push the changes, the Action will execute and later you will receive a notification email from NPM. That's really exciting!

(2020/7/23) Setting up ESLint to work with TypeScript (

  1. npm install eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin -D.
  2. npx eslint --init, choose a popular styling like airbnb, json format.
  3. "comma-dangle": ["error", "never"], "no-console": "off", "arrow-parens": "off", "linebreak-style": "off".
  4. npm install prettier -D. Install prettier extension in VSCode. "File" -> "References" -> "Settings" -> search "Format On Save".
  5. npm install -D eslint-config-prettier. Create a local configure file '.prettierrc'. Example project:

If you want to upgrade all dependencies, should be very careful, please follow:

  1. npm outdated, give a list.
  2. npm install -g npm-check-updates, install the tool.
  3. ncu -u, update the version.
  4. npm update / npm install

Here is the help link about how to create a template repository and how to use it:

Top comments (0)