Website Links

Monday, 12 November 2018

Sharing JS Code Across BitBucket Repositories

Recently, I have been writing APIs in node.js with the express framework, a React website and a React Native app that have some shared code. These are spread across several repositories, so I needed a way to share code across the repositories. So I created a node package in another repository.

Then I needed to figure out how to access the repository and install my package via npm. Fortunately, this proved to be relatively easy… Go to your account's BitBucket Settings > App Passwords. From here, you can create an app password and grant read access to your repositories. Then all you need to do is go to your package.json and add to your dependencies e.g.

  {
    …
    dependencies: {
      …,
      "[PACKAGE_NAME]": "git+https://[BITBUCKET_USERNAME]:
               [APP_PASSWORD]@bitbucket.org/PROJECT_NAME/
               REPOSITORY_NAME.git#BRANCH_NAME"
    },
    …
  }


If you then execute the "npm install" command this should install the repo to your node_modules folder.

But what happens when you push changes to the common repository? How do I run the code with the latest commits? Simply add an additional line to your package.json scripts:

  {
    …
    scripts: {
      …,
      "clean-start": "rm -rf -- node_modules/[PACKAGE_NAME]; 
                      npm install; npm start;"
    },
    …
  }


Then when you want to run with the latest changes use
npm run clean-start
instead of
npm start

No comments:

Post a Comment