- Having an account in npmjs.com
- Having an account github.com
make a directory for your module:
mkdir my-module`
navigate it:
cd my-module
run npm init
there are some questions like these:
package name
: insert your package name. see Package name guidelinesversion: (1.0.0)
: for choosing default press enter.description
: a short description about your packageentry point: (index.js)
: your module entry point, press enter to select default.test command
: leave blankgit repository
: insert your module git repository address.keywords
: insert relative keyboards of your module if you want.license (ISC)
: leave it if you don't want change it,- finally press enter or type
yes
to accept creatingpackage.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://[email protected]/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
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/[email protected]
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/[email protected]
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