Search code examples
node.jstypescriptnpmtsconfig

How to set up my native node module if it is imported by my host project?


I am developing 2 projects in TypeScript. First, a native node module called my-node-module. It is imported by my host project called host-project.

enter image description here

package.json of host-project

"my-node-module": "git+ssh://git@github.com:foo/my-node-module"

my-node-module is written in TypeScript. I want to import it within my host project.

Question 1

The main entry in the package.json of my-node-module is js/index.js. This file is created if I call npm run tsc in the root of my-node-module.

Can I directly import the project (src/index.ts) file via import myModule from 'my-node-module' or is the transpile step really necessary?

Question 2

If the question above is answered with yes, how would I ensure that npm i on the host project also executes the required transpile steps for my dependency module my-node-module?


Solution

  • I use similar approach in one of my commercial project. I have some common code used by all,

    1. React JS frontend
    2. React Native frontend
    3. Express js based graphql server

    Then there's another package which has UI components ready to import in React JS web and React Native mobile projects.

    All of these projects are install as npm packages and hosted in remote git repos ( In my case, they are on remote Bitbucket repositories )

    For example, I have following configuration for my shared logic project.

    package @myorg/common-logics

    {
      "name": "@myorg/common-logics",
      "version": "1.4.0",
      "main": "./build/index.js",
      "types": "./build/index.d.ts",
      "private": true,
      "files": [
        "build/**/*"
      ],
      "author": "Dilshan",
      "license": "MIT",
      "scripts": {
        "clean": "rimraf build",
        "build": "yarn run clean && tsc",
        "test": "npx ts-node src/test/data.ts",
        "version-upgrade": "yarn version patch",
        "changelog": "standard-version"
      },
      "devDependencies": {
        "@types/lodash": "^4.14.177",
        "@types/uuid": "^8.3.3",
        "rimraf": "^3.0.2",
        "standard-version": "^9.3.2",
        "ts-node": "^10.4.0",
        "typescript": "^4.5.2"
      },
      "dependencies": {
        
      },
      "repository": {}
    

    Previously I manually built this project by running yarn build because I was the only one works on this shared package. You can also use Husky or a pipeline as well.

    If I had to push this to a npm registry, i would have built the code in a pipeline.


    This worked until I had to manage multiple npm packages. It was getting harder to keep track all of them so Here I used https://nx.dev/ to manage all of my npm packages. Then I created separate repositories ( Because I didn't used a NPM registry ) just to hold the build code of each packages in NX mono repo.

    So all I do is, do my development in NX mono repo and then push it to the repository. Then in a pipeline I build the libraries in NX project and push them to the read only repositories that just hold the latest build code of the source code.

    I can say it works & currently the production system uses these shared libraries and I have been working with these custom shared libraries over 6 - 7 months now. I never had any issues unless this one problem with NX. Also never had a source code - build code sync issue so far.

    If you cannot setup CI/CD pipelines, I guess you can do the same thing in your local machine with Husky and custom shell script to build and copy build code to readonly repos.