Skip to content

Instantly share code, notes, and snippets.

@arturohernandez10
Last active May 20, 2021 23:20
Show Gist options
  • Save arturohernandez10/a0ff598f6519c0841c437e2e7d50da7a to your computer and use it in GitHub Desktop.
Save arturohernandez10/a0ff598f6519c0841c437e2e7d50da7a to your computer and use it in GitHub Desktop.
How to create and distribute a new (private) Javascript library

How to create and distribute a new Javascript library

In this guide we use a really good typescript library starter package, with the nondescript name of typescript-starter. Since typescript is a superset of javascript with better IDE support. It includes CI/CD configuration, publishing, documentation and release notes documentation. So it is a great start.

Project and repository setup

Run the following command line and follow the prompts. Make sure to name the project with this convention @{{OWNER}}/library-name

npx typescript-starter

Create a git repository. For this guide we use github, since npm is now owned by github and the npm registry is sunsetted.

git remote add origin https://@{{OWNER}}/library-name

git branch -M main

git push -u origin main

Registry

For Github packages. Modify your global or local .npmrc file to include the following two lines

@{{OWNER}}:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken={{TOKEN}}

And add the following to your library package.config

 "publishConfig": {
   "registry": "https://npm.pkg.github.com/"
 },

Where the token is a github token with read and write access. Other users will need to do the same, adding their own tokens. Here is a link to github for convenience: https://github.com/settings/tokens. Of course if the library is public you can skip the token step.

Publish

The typescript-starter documentation is very thorough, I recommend you read it to see what other scripts and features are included. To publish you will need to make sure that all your commits are up to date and then type:

npm run prepare-release

Then you will have to push your code to github. And publish your package to github as well in this case.

git push --follow-tags && npm publish

.npmrc file

To find the location of the global .npmrc file use this command.

npm config list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment