Tuesday, July 2, 2019

Creating Organisation Scoped Public NPM Packages

NPM has a really cool feature called Scoped Packages. My understanding is that it lets you group packages together so you can organise code into monorepos

Here is how I create Scoped Packages inside NPM Organisations. I use this as a reference for my side projects.


Build you GIT project

1) First you need a GIT account. e.g.
https://github.com/newbreedofgeek

2) I doubt this is needed, but you may need to create an organisation through your personal user account. e.g.
https://github.com/Itheum

3) Create your GIT project locally and build your project that you want to make into a NPM Organisation Scoped Public Module. e.g.
https://github.com/Itheum/react-schedule-it


Once you are ready to make it public you need to do the following related to NPM

1) Login into your NPM account here https://www.npmjs.com or create a new account

2) Create a new Organisation here https://www.npmjs.com/org/create (this link may change, just follow the UI for the option to create an organisation). Public orgs are free or you can have paid private orgs

3) Enter a new Organisation Name. Basically this becomes your "scope"

4) After your Organisation was created you need to associate your GIT project to this organisation scope


Publish your GIT Project as a Organisation Scoped Public Module

1) Go back to your GIT project and update your package.json file to have a scoped name. e.g.
"name": "@itheum/react-schedule-it"

2) Make sure the repository points to the correct endpoint in github. e.g.
"repository": {
    "type": "git",
    "url": "git+https://github.com/Itheum/react-schedule-it.git"
  }

3) If the above manual steps does not work just follow the steps here and recreate your package.json automatically https://docs.npmjs.com/creating-and-publishing-scoped-public-packages by running this command e.g.
npm init --scope=@my-org

4) After this is done, I checked in my code to github and then published to NPM via the command
npm publish --access public

And that's it. My project became a Organisation Scoped Public Module. e.g.
https://www.npmjs.com/package/@itheum/react-schedule-it

I can now keep all my code in Github is a central account but scope my NPM modules by organisation. Its also super handy to use this design with something like Lerna to create monorepos.

Happy Coding!

No comments:

Post a Comment

Fork me on GitHub