How to automate package publishing to npm registry
Objectives of this guide
Upon completing this guide you'll be able to:
- Create an npm package
- Publish the package in the registry
- Automate testing and publishing of the npm package with Buddy
Action used in this guide:
Requirements
Before you start you need to install the following things:
Creating an npm package
Start off with creating a folder and initializing a Git repository so that your package is safe and sound under version control:
default$ mkdir my_first_npm_module $ cd my_first_npm_module $ git init
The next step is adding package.json
which will contain the data of your module. The easiest way to do that is running this command:
default$ npm init
You will be asked to enter the information about your package:
name
version
main value
(usually set toindex.js
)
When you're ready, check the contents of package.json
. It should look like this:
json{ "name": "buddy-demo-package", "version": "1.0.3", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
In index.js
the functions should be provided as a property of the exports
object. Here's an example:
jsexports.printMsg = function() { console.log("My first package message"); }
Once everything is set, you should commit your files to the repo:
default$ git add . $ git commit -m 'init my package'
Publishing the package
In order to publish the package, you need to sign up to the npm registry. You can do it in two ways:
- Sign up at npmjs.com
- Execute
npm adduser
in the terminal
Once you've registered, sign in to the client and publish the package:
default$ npm login $ npm publish
You can check if everything worked properly by looking up your package at npmjs.com by the name provided in the package.json
.
npm publish
command publishes all files from the directory, except those added to .gitgnore
and .npmignore
.
Making changes to the package
Whenever you make changes to your package (add a new feature, etc.) you need to update the version in the package.json
. The most convenient way is to use the npm version
method with either minor
or major
patch type. Then, run npm publish once
again:
default$ npm version patch $ npm publish
Automating npm tests and publishing
A good practice before sending the package to the registry is to test it so you know it's free of errors. You can do that by setting a pipeline in Buddy that will both test and deploy your package.
When you're done, sign in to your Buddy workspace and do the following:
- Create a new project, choose GitHub as the provider, and select the forked repository
- Add a new pipeline, set the trigger mode to On every push and select Master as the target branch.
- Add the Node.js action. The default commands will run: Image loading...
Add another Node.js action that will push the package to the registry: Image loading... Make sure to install
cli-login
in the Environment tab so you can authenticate:defaultnpm install -g npm-cli-login publish
Image loading...
Once the pipeline is set, Buddy will automatically test your package on every push to the Master branch and, if there are no errors, deliver it to the registry.
Using environment variables to store npm credentials
Buddy lets you store authentication credentials as environment variables that you can later use in actions across the whole workspace, project or pipeline.
- Go to the Project Options and open Environment variables tab
- Enter
NPM_PASSWORD
,NPM_EMAIL
andNPM_LOGIN
and their corresponding values: Image loading...
Feel free to share this guide, and if you need any help in configuring and automating your process, just drop a word to support@buddy.works.
Jarek Dylewski
Customer Support
A journalist and an SEO specialist trying to find himself in the unforgiving world of coders. Gamer, a non-fiction literature fan and obsessive carnivore. Jarek uses his talents to convert the programming lingo into a cohesive and approachable narration.