跳到主要内容

如何自动打包并发布至npm注册中心

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:

$ 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:

$ npm init

You will be asked to enter the information about your package:

  • name
  • version
  • main value (usually set to index.js)

When you're ready, check the contents of package.json. It should look like this:

{
"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:

exports.printMsg = function() {
console.log("My first package message");
}

Once everything is set, you should commit your files to the repo:

$ 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:

  1. Sign up at npmjs.com
  2. Execute npm adduser in the terminal

Once you've registered, sign in to the client and publish the package:

$ 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.

信息

The 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:

$ 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:

  1. Create a new project, choose GitHub as the provider, and select the forked repository

  2. Add a new pipeline, set the trigger mode to On every push and select Master as the target branch.

  3. Add the Node.js action. The default commands will run: Defining tests [Click to enlarge]

  4. Add another Node.js action that will push the package to the registry: Setting action details [Click to enlarge] Make sure to install cli-login in the Environment tab so you can authenticate:

        npm install -g npm-cli-login publish

    Setting action details [Click to enlarge]

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.

  1. Go to the Project Options and open Environment variables tab
  2. Enter NPM_PASSWORD, NPM_EMAIL and NPM_LOGIN and their corresponding values: Defining environment variables [Click to enlarge]
提示

You can check Encrypt to hide sensitive data from other users.

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 or leave a message at our community forum.