AfterAcademy Tech
•
25 Jul 2020

npm(node package manager) is regarded as the standard package manager used in javascript. The npm registry crossed a million packages last year(Jun ’19). It is the largest single-language code repository. That being said, it is a little obvious now that npm is a big deal.
npm is automatically installed when you install Node.js
Since its a package manager, it is used to manage downloads and handle dependencies of your project.
It is a common saying that
There is a package for everything in npm
Well, almost everything. There’s always more.
Let’s have a walkthrough of npm from introduction to how to publish your own package.
npm is written entirely in JavaScript(JS) and was developed by Isaac Schlueter. It was initially used to download and manage dependencies, but it has since also used frequently in frontend JavaScript.
npm can manage packages that are local dependencies of a particular project, as well as globally-installed JavaScript tools. In addition to plain downloads, npm also manages versioning, so you can install any version, higher or lower according to the needs of your project. If no version is mentioned, the latest version of the package is installed.
→ If package.json file exists in your project directory, all you need to do is use this command
npm installThis command will initialize the node_modules folder and install all packages that the project needs.
And if you need to update the installed packages, hit
npm updateAll packages will be updated to their latest versions.
→ If you just need to install a single package, you can use this command
npm install <package_name>
Similarly, if you just need to update a single package, all you need to do is
npm update <package_name>
Note: By default, packages are installed in the local scope. If you need to install the package at global scope, you need to mention the flag -g
npm install <package_name> -g
This will install the package at the global scope in the system directory.
Several developers feel the need to use some functionality of one project in another. Normally, the developer copies code from one project and pastes in another but if it is common functionality that may be used in several projects its a better and good practice to publish your reusable codes as npm packages.
Also, why miss a good opportunity to become an author, right?
Okay, so let’s start!
When you are in your desired directory, open the CLI, and use this command:
npm initA text utility appears and you are asked to enter a few details to create a basic package.json file such as package name, version, description, author, etc.
Enter the details of the package as you please. The defaults will be mentioned in parentheses like this →
package name: (test-pkg)
version: (1.0.0)
description: This is a test package
...
Just hit Enter if you want the default options.
A basic package.json will be created looking something like this →
{
"name": "test-pkg",
"version": "1.0.0",
"description": "This is a test package",
"main": "index.js",
"author": "after-academy",
"license": "ISC"
}
Obviously, you can change the package.json file later if you want.
Now, you need to prepare the source code. If the code is not too big, it is typically just write in the main file(index.js here). Else, conventionally a src directory is used where your abstract code is stored in several files.
★ Remember to export the code using module.exports
You now need to thoroughly test your code before publishing. This is how you, as a developer, confirm that your package can actually be used.
If this is your first time, you probably won’t be logged in with your npm account. If you don’t have an account, go to npmjs.com and create your account.
Now, in your terminal, log in with your npm account.
npm loginAnd now to publish your package,
npm publishAnd you’re done!
You can now check your published project at www.npmjs.com/<username>/<pkg-name>.
The details regarding the package such as version, author name, description, etc. are extracted from the package.json file you created earlier.
Now you’ve got a basic idea about what npm is and how you can publish your own package to the npm registry.
Please give your feedback in the comments below.
AfterAcademy Tech
npm and Yarn are two very popular and highly used package managers and we discuss on the comparisons between the two in this blog

AfterAcademy Tech
The idea of this blog is to discuss the famous data structure called Array. An Array is one of the important data structures that are asked in the interviews. So, we will learn about arrays and we will also discuss about the idea of the dynamic array and its amortized analysis. So let's get started.

AfterAcademy Tech
In this blog, we will learn about the Graph data structure that is used to store data in the form of vertices and edges. We will also see some of the properties, types, representation of graph.

AfterAcademy Tech
Data structures are widely used in every aspect of computer science. Data structures are the way of organizing and storing data in a computer so that it can be used efficiently. In this blog, we will look into data structures, its types, operations and applications.
