In this article, we will learn about easy steps for creating and publishing your javascript or node js utility to npm package repository.
How did I get the idea for creating and publishing of npm packages?
Actually, I was working on creating a dashboard with charts. As most library charts have their own convention for passing data to generate charts e.g. tui-charts, highcharts etc., while preparing data I had to rename some keys as per the required convention.
I have to rename multiple keys from an array of an object. In C# or Dot Net, you have probably heard about the term "DTO - Data Transfer Object" or Automapper which provides this functionality in which it converts one class to another class type with required fields.
Here, I have searched for javascript and JSON utility library using underscore js and lodash as well, but I didn't find any solution, so I created a utility function and published it on npm. So I can utilize this in my different projects and as it is on npm it can be useful to other developers as well.
Prerequisite
- Nodejs and npm must be installed on System
- A user must have a GitHub account on which he can store source code.
- A user must have an account on npmjs
Step 1 - Create node module using npm init command
If you are using Windows system then you can open a command prompt with appropriate rights, or if you are a Linux user then you can open a terminal and run npm init command,
- npm init
Note
Make sure your package name is unique in npm.js and it is available on npm.
Using npm init command one "package.json file" is created in a project folder.
Take a look at my package.json file for reference purposes,
Take a look at my package.json file for reference purposes,
- {
- "name": "dto-utils",
- "version": "0.0.1",
- "description": "Inspired from Dot Net Data Transfer Object Concept, Utility contains method which rename keys in existing object using plain Javascript code.",
- "main": "index.js",
- "scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
- },
- "keywords": ["dto", "rename keys", "json", "utility", "utils", "transform object"],
- "author": "anomepani",
- "license": "ISC"
- }
Step 2 - Open current directory in any text editor and add entry point file index.js.
I am using Visual Studio Code as the text editor. To open a project or current project directory in vs code, type the below command or you can just right click on a folder and open in your favorite IDE or Text Editor.
- code .
Now create an index.js file which is an entry for any npm package and add your utility method or code to an index.js file.
Take a look at my utility method or index.js file.
Take a look at my utility method or index.js file.



Join the conversation! Your thoughts help the community grow.