DEV Community

sijils
sijils

Posted on

Publishing npm package to Github package repository

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
Enter fullscreen mode Exit fullscreen mode

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=https://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 and github npm module at https://github.com/sijils/resume and https://github.com/sijils/resume/packages respectively for reference.

Oldest comments (7)

Collapse
 
joeattardi profile image
Joe Attardi

Is there an easy way to publish a package both to the GitHub registry and the npm registry that you know of?

Collapse
 
lildata profile image
Programming is fun

Thanks! the guide in Github is so bad, with yours it was done in 2 minutes :-)

Collapse
 
slashmili profile image
Milad

What I couldn't figure out is how to login with npm command line if my account has two factor auth on!

Collapse
 
awdynamic profile image
awdynamic

This is just what I was looking for. But how do I build the .npmrc on heroku.

Collapse
 
rahulbordoloi profile image
Rahul Bordoloi

I was looking for this for like Hours! Thanks for the same, Really Simple and Good Work :)

Collapse
 
pprathameshmore profile image
Prathamesh More

I am getting this error

npm ERR! code E404
npm ERR! 404 Not Found - PUT https://npm.pkg.github.com/quotegarden
npm ERR! 404
npm ERR! 404  'quotegarden@1.0.0' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

Enter fullscreen mode Exit fullscreen mode
Collapse
 
bratorimatori profile image
Bojan Tomic

Why is this .npmrc necessary?