Skip to content

Instantly share code, notes, and snippets.

@meysam-mahmoodi
Last active December 19, 2018 07:03
Show Gist options
  • Save meysam-mahmoodi/5019a7dc8d564b8df521af14b75c9c8e to your computer and use it in GitHub Desktop.
Save meysam-mahmoodi/5019a7dc8d564b8df521af14b75c9c8e to your computer and use it in GitHub Desktop.
How to craete a npm module and publish it

How to create a npm module and publish it

Requirements

  • Having an account in npmjs.com
  • Having an account github.com

Make a NPM module

make a directory for your module: mkdir my-module` navigate it: cd my-module run npm init there are some questions like these:

  1. package name: insert your package name. see Package name guidelines
  2. version: (1.0.0) : for choosing default press enter.
  3. description: a short description about your package
  4. entry point: (index.js) : your module entry point, press enter to select default.
  5. test command : leave blank
  6. git repository : insert your module git repository address.
  7. keywords : insert relative keyboards of your module if you want.
  8. license (ISC) : leave it if you don't want change it,
  9. finally press enter or type yes to accept creating package.json file.

you will have a package.json file with following example:

{
  "name": "@meysam1369/justfortest",
  "version": "1.0.0",
  "description": "a package for test",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://git@github.com/meysam-mahmoodi/justfortest.git"
  },
  "keywords": [
    "nativescript"
  ],
  "author": "Meysam Mahmoudi",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/meysam-mahmoodi/justfortest/issues"
  },
  "homepage": "https://github.com/meysam-mahmoodi/justfortest#readme"
}

make index.js file with following content inside your module:

exports.printMsg = function() {
  console.log("This is a message from the demo package");
}

congrats! your module is ready.

also you can see + this video

Publish module

To publish your module to npmjs.com, follow these instructions: your module url will be like this format:

https://www.npmjs.com/package/@your-username/your-package-name

So, you should chose a suitable and unique name for your package like this: @meysam1369/justfortest

Login to your npmjs.com account on command line: npm login Insert your username & password there.

Navigate to module root, then run : npm publish --access=public If you see a result like following, your module have published it successfully:

$ npm publish --access=public
npm notice
npm notice package: @meysam1369/justfortest@1.0.0
npm notice === Tarball Contents ===
npm notice 559B package.json
npm notice 93B  index.js
npm notice === Tarball Details ===
npm notice name:          @meysam1369/justfortest
npm notice version:       1.0.0
npm notice package size:  485 B
npm notice unpacked size: 652 B
npm notice shasum:        efcc4f804742ed38855474437dc0b70b776dd67b
npm notice integrity:     sha512-gLQykr+XBcyJp[...]xGymJc/3cp4TA==
npm notice total files:   2
npm notice
+ @meysam1369/justfortest@1.0.0

Use your module

1- On the command line, create a new test directory outside of your project directory. mkdir test-directory 2- Switch to the new directory: cd test-directory 3- In the test directory, install your module:

npm install <your-module-name>
## npm install @meysam1369/justfortest

4- In the test directory, create a test.js file which requires your module and calls your module as a method.

var req = require('@meysam1369/justfortest');
req.printMsg();

5- On the command line, run node test.js. The message sent to the console.log should appear.

This is a message from the demo package
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment