Sijil Salim
Live By The Code

Follow

Live By The Code

Follow
Publishing npm package to Github package repository

Photo by Roman Synkevych πŸ‡ΊπŸ‡¦ on Unsplash

Publishing npm package to Github package repository

Sijil Salim's photo
Sijil Salim
Β·Jun 24, 2022Β·

2 min read

Github package repository is where you can publish npm, gem, mvn, nuget, gradle, docker packages and is currently now in beta. In this tutorial, we will see how we can publish an npm package to the github package respository.

  • Code the node.js project you want to publish as npm module in github repo.
  • Create a github repository in which you will be publishing the package.
  • After you have done the programming for the node.js project, modify the name field and add the fields bin, repository and publishConfig to the package.json as shown below.
"name": "@github_user/npm_module_name"
"bin": {
    "<cli_command_name>": "./<name_of_the_index_script>"
},
"repository": {
    "type": "git",
    "url": "<git_repo_link>"
},
"publishConfig": {
    "registry": "https://npm.pkg.github.com/"
}

cli_command_name is the cli command for invoking your module. name_of_the_index_script is the script that will be called when cli_command_name is invoked. It is the main script in your project. git_repo_link is the git repo you have created for publishing the npm module. npm_module_name is the name of the npm module to be published.

  • Login to the github npm registry from the console using the below command
    npm login --registry=https://npm.pkg.github.com --scope=@github_user

github_user is the username of your github account. Executing this query will ask for username and password. Username is your github username. Password is Personal Access Token which can be generated from your github account settings page.

  • Run npm publish command from the project directory. This will publish your node.js project as npm module to github repository. The github repository link is git_repo_link/packages . The module will be published in the scoped mode.
  • A git push will push the project to the git repo mentioned in the package.json file, git_repo_link

Now the module can be installed by anyone from this repository. To do this, the user who intends to install the module should set the npm registry in .npmrc file as registry=npm.pkg.github.com/github_user . Once this configuration is done, the npm module can be installed by running the command npm install @github_user/npm_module_name

You can find my github repo here and github npm module here for reference.

Β 
Share this