<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Salman Arefin</title>
    <description>The latest articles on DEV Community by Salman Arefin (@salmanarefin74).</description>
    <link>https://dev.to/salmanarefin74</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1290206%2Fb06d6134-67d5-4e90-85a8-7b2109807263.jpg</url>
      <title>DEV Community: Salman Arefin</title>
      <link>https://dev.to/salmanarefin74</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/salmanarefin74"/>
    <language>en</language>
    <item>
      <title>npm and everything you need to know about the package.json</title>
      <dc:creator>Salman Arefin</dc:creator>
      <pubDate>Wed, 27 Nov 2024 10:23:09 +0000</pubDate>
      <link>https://dev.to/salmanarefin74/npm-and-everything-you-need-to-know-about-the-packagejson-33f1</link>
      <guid>https://dev.to/salmanarefin74/npm-and-everything-you-need-to-know-about-the-packagejson-33f1</guid>
      <description>&lt;h2&gt;
  
  
  What is npm?
&lt;/h2&gt;

&lt;p&gt;npm is a package manager for NodeJS. It is also the largest single language code repository on earth and a tool for installing and managing packages from the repository on the command line.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a package?
&lt;/h2&gt;

&lt;p&gt;The npm registry consists of numerous packages or libraries that can be downloaded, installed, and used as a dependency in a NodeJS project. An npm package is a reusable piece of code published to the npm registry. It helps developers improve their workflow by incorporating functionality, thereby reducing the need to write redundant or repetitive code.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do I install a package in my NodeJS project?
&lt;/h2&gt;

&lt;p&gt;By using the CLI command &lt;code&gt;npm install&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm install&lt;/code&gt;: This command will install all the dependencies mentioned in the &lt;code&gt;package.json&lt;/code&gt; in the &lt;code&gt;node_modules&lt;/code&gt; folder.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install &amp;lt;package-name&amp;gt;&lt;/code&gt;: Installs the package in the current project directory (inside the node_modules folder). The package is accessible only within that project.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install -g &amp;lt;package-name&amp;gt;&lt;/code&gt;: Installs the package system-wide, making it available from anywhere on your machine.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install &amp;lt;package-name&amp;gt;@&amp;lt;version-number&amp;gt;&lt;/code&gt;: Installs a specific version of that package.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install &amp;lt;package-name&amp;gt; --save-dev&lt;/code&gt;: Installs the package and puts it in the &lt;code&gt;devDependencies&lt;/code&gt; block of &lt;code&gt;package.json&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install &amp;lt;package-name&amp;gt; --no-save&lt;/code&gt;: Installs the package but does not add the entry to the &lt;code&gt;package.json&lt;/code&gt; file dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install &amp;lt;package-name&amp;gt; --save-optional&lt;/code&gt;: Installs the package and adds the entry to the package.json file's &lt;code&gt;optionalDependencies&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm install &amp;lt;package-name&amp;gt; --no-optional&lt;/code&gt;: This will prevent the installation of optional dependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is package.json?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;package.json&lt;/code&gt; is a configuration file used in Node.js projects to manage project metadata, dependencies, and scripts. It acts as the heart of a NodeJS project.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the difference between &lt;code&gt;devDependencies&lt;/code&gt; and &lt;code&gt;peerDependencies&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;devDependencies&lt;/strong&gt;: These are packages and libraries needed only during development or testing. They are not included in the production code.&lt;/p&gt;

&lt;p&gt;Installation:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install tslint --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;peerDependencies&lt;/strong&gt;: These are dependencies that the project needs to work on, but it expects the user who is installing the package to provide the dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"peerDependencies": {
  "graphql": "&amp;gt;=10.0.0"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above block means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The project needs the package &lt;code&gt;graphql&lt;/code&gt; to work.&lt;/li&gt;
&lt;li&gt;It needs the version of the &lt;code&gt;graphql&lt;/code&gt; package to be &lt;code&gt;10.0.0&lt;/code&gt; or higher.&lt;/li&gt;
&lt;li&gt;The package users must install GraphQL themselves.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Scripts in package.json
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;scripts&lt;/code&gt; field in &lt;code&gt;package.json&lt;/code&gt; defines commands that can be run using &lt;code&gt;npm run &amp;lt;script-name&amp;gt;&lt;/code&gt;. Some scripts worth mentioning:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;start&lt;/strong&gt;: The command to start the application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"start": "node index.js"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;build&lt;/strong&gt;: Used for production builds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"build": "webpack --mode production"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;test&lt;/strong&gt;: Runs the unit test suite.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"test": "nyc"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;dev&lt;/strong&gt;: Starts the development server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"dev": "nodemon index.js"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;lint&lt;/strong&gt;: Runs a linter to check code quality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"lint": "tslint ."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;clean&lt;/strong&gt;: Cleans up build artefacts.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"clean": "rm -rf dist"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;compile&lt;/strong&gt;: Used to transpile source code into a different format (e.g., TypeScript to JavaScript)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"compile": "tsc"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;publish&lt;/strong&gt;: Used to publish the package to a registry like npm.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"publish": "npm publish"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pre/Post Hooks&lt;/strong&gt;: There are also pre/post hooks for scripts like preinstall, postinstall, prebuild, precompile, postpublish etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Custom scripts&lt;/strong&gt;: Custom scripts can also be written in the &lt;code&gt;package.json&lt;/code&gt; and can be just run using &lt;code&gt;npm run &amp;lt;script-name&amp;gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  npm package versioning
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Major&lt;/strong&gt;: When a feature is added with a breaking change in the functionality.&lt;br&gt;
&lt;strong&gt;Minor&lt;/strong&gt;: When a feature is added in a backward-compatible manner.&lt;br&gt;
&lt;strong&gt;Patch&lt;/strong&gt;: When a bug is fixed, which doesn't break any backward compatibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Version: 1.2.3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here, 1 is major, 2 is minor, and 3 is patch.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>npm</category>
      <category>webdev</category>
    </item>
    <item>
      <title>NodeJS Modules [Simple Explanation]</title>
      <dc:creator>Salman Arefin</dc:creator>
      <pubDate>Sun, 24 Nov 2024 07:44:12 +0000</pubDate>
      <link>https://dev.to/salmanarefin74/nodejs-modules-simple-explanation-4i6l</link>
      <guid>https://dev.to/salmanarefin74/nodejs-modules-simple-explanation-4i6l</guid>
      <description>&lt;h2&gt;
  
  
  What are modules?
&lt;/h2&gt;

&lt;p&gt;Imagine you have an &lt;code&gt;index.js&lt;/code&gt; file in your NodeJS project where you have used five functions. However, two of these functions might also be used in other files. So, instead of one file, you create three files in your project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;index.js&lt;/li&gt;
&lt;li&gt;dependency1.js&lt;/li&gt;
&lt;li&gt;dependency2.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, each of these Javascript files is a module, and the way to export classes/functions and import them is basically the module system. A module system allows us to split and include code and import code written by us or other developers whenever required.&lt;/p&gt;

&lt;p&gt;These modules are not just the Javascript files that exist in your project; they can also be any external package you have installed as a dependency in your project. Also, NodeJS has some built-in modules like &lt;code&gt;http&lt;/code&gt;, &lt;code&gt;fs&lt;/code&gt;, etc., which are available along with installation and can be imported without adding any external dependencies.&lt;/p&gt;

&lt;h2&gt;
  
  
  How can I export or import a module?
&lt;/h2&gt;

&lt;p&gt;Two module systems are used in Node. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;CommonJS (CJS)&lt;/li&gt;
&lt;li&gt;ECMAScript Module (ESM)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You have read the kitchen-chef-waiter example in my previous &lt;a href="https://dev.to/salmanarefin74/introduction-to-nodejs-simple-explanation-37f3"&gt;blog&lt;/a&gt;; Similarly, if we compare modules with that, imagine CommonJS is an old big recipe book we needed to search and find a recipe, while ESM is a new aged digital app to see recipes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CommonJS (CJS)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The older module system.&lt;/li&gt;
&lt;li&gt;Modules are written in a simple format like:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Export
module.exports = function SayHello() {
  console.log("Hello World!");
};

// Import
const GetHello = require("./hello-script.js");
SayHello(); // "Hello World!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Synchronous: Loads modules one after another.&lt;/li&gt;
&lt;li&gt;Works only in Node.js (not directly in browsers).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;ECMAScript Modules (ESM)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The modern module system.&lt;/li&gt;
&lt;li&gt;Modules are now more structured:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Export
export function SayHello() {
  console.log("Hello World!");
}

// Import
import { SayHello } from "./hello-script.js";
SayHello();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Asynchronous: Loads multiple modules at the same time.&lt;/li&gt;
&lt;li&gt;Works natively in browsers and Node.js.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Difference in syntax:&lt;/strong&gt;&lt;br&gt;
CJS: &lt;code&gt;module.exports&lt;/code&gt; / &lt;code&gt;require&lt;/code&gt;&lt;br&gt;
ESM: &lt;code&gt;export&lt;/code&gt; / &lt;code&gt;import&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How do I configure CJS or ESM in my project?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Open the &lt;code&gt;package.json&lt;/code&gt; of your project.&lt;/li&gt;
&lt;li&gt;Add:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  type: "module";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;This tells NodeJS to interpret &lt;code&gt;.js&lt;/code&gt; as ESM. If we don't add this, NodeJS will interpret it as CommonJS by default.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Modules in Typescript:
&lt;/h2&gt;

&lt;p&gt;Sometimes, you can use ECMAScript modules but an old package you have imported is written in CommonJS. To handle these cases, we sometimes make sure the output Javascript code generated from the Typescript file comes in the &lt;code&gt;common&lt;/code&gt; format, even though we have written the Typescript files in ESM format.&lt;/p&gt;

&lt;p&gt;For this, we add the &lt;code&gt;compilerOptions&lt;/code&gt; in the &lt;code&gt;tsconfig.json&lt;/code&gt; of our project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"compilerOptions": {
  "module": "commonjs",
  "target": "es6"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What happens then:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;module: "commonjs"&lt;/code&gt;: Outputs JavaScript using the CommonJS module system, which uses &lt;code&gt;require&lt;/code&gt; and &lt;code&gt;module.exports&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;target: "es6"&lt;/code&gt;: Ensures that the output JavaScript uses ES6 syntax and features like &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, and arrow functions.&lt;/p&gt;

&lt;p&gt;Input Typescript code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { readFile } from "fs";

const SayHello = () =&amp;gt; {
  console.log("Hello World!");
};

export { SayHello };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output Javascript code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SayHello = void 0;
const fs_1 = require("fs");
const SayHello = () =&amp;gt; {
    console.log("Hello World!");
};
exports.SayHello = SayHello;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>node</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>backend</category>
    </item>
    <item>
      <title>Introduction to NodeJS [Simple Explanation]</title>
      <dc:creator>Salman Arefin</dc:creator>
      <pubDate>Fri, 22 Nov 2024 08:39:57 +0000</pubDate>
      <link>https://dev.to/salmanarefin74/introduction-to-nodejs-simple-explanation-37f3</link>
      <guid>https://dev.to/salmanarefin74/introduction-to-nodejs-simple-explanation-37f3</guid>
      <description>&lt;h2&gt;
  
  
  What is NodeJS?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An open-source and cross-platform Javascript runtime environment.&lt;/li&gt;
&lt;li&gt;Runs on the Javascript V8 engine (which is the core of Google Chrome)&lt;/li&gt;
&lt;li&gt;Single-threaded and uses asynchronous programming.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  If NodeJS is single-threaded, how does it handle multiple requests?
&lt;/h2&gt;

&lt;p&gt;Imagine you go to a restaurant where there is only a single chef. You order Pasta from the waiter. The waiter informs the chef about the order and other customer orders. The chef gets your Pasta order but realizes it will take time to boil your Pasta. Instead of waiting for your Pasta to cook, it keeps it in the stove to boil and meanwhile starts processing the other orders/&lt;/p&gt;

&lt;p&gt;NodeJS works similarly. &lt;br&gt;
&lt;strong&gt;Chef&lt;/strong&gt; = Node.js' Main Thread (Node.js can only do one thing at a time on its main thread)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Waiter&lt;/strong&gt; = Event Loop (The event loop takes incoming requests and decides how to handle them)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kitchen Helpers&lt;/strong&gt; = Worker Threads or OS Background Processes (Node.js delegates the work to helpers)&lt;/p&gt;

&lt;p&gt;For example, in an I/O operation, when the web server is requested to fetch content from a file, NodeJS simply delegates the work to the OS's file system and gets ready to handle the subsequent request. When the file system responds, it handles that.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why should I choose NodeJS over other backend applications?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Same language across the stack&lt;/strong&gt;: Most front-end development uses JS frameworks like Angular, React, Vue, etc. It would be a massive advantage if the backend application were written in Javascript. Full-stack developers don't need to learn a new language for backend development.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Ideal for microservice-based architecture&lt;/strong&gt;: NodeJS's single-threaded event-loop architecture is much more convenient for web developers than multi-threaded architecture, as there is no risk of available threads being finished when handling multiple concurrent requests.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Fast development&lt;/strong&gt;: NodeJS is very lightweight, and NodeJS servers can be up and running quickly with little effort.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;NPM&lt;/strong&gt;: The NodeJS Package Manager is a vast library of code developers write worldwide. It can easily be used as a dependency in a project, shortening the effort to write extra code.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  "NodeJS servers can be up and running quickly using little effort" - is it that simple?
&lt;/h2&gt;

&lt;p&gt;YES.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { createServer } = require('node:http');

const hostname = '127.0.0.1';
const port = 3000;

const server = createServer((req, res) =&amp;gt; {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, hostname, () =&amp;gt; {
  console.log(`Server running at http://${hostname}:${port}/`);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this code does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uses the &lt;code&gt;http&lt;/code&gt; module of NodeJS.&lt;/li&gt;
&lt;li&gt;Configures the host and port.&lt;/li&gt;
&lt;li&gt;Uses the &lt;code&gt;createServer&lt;/code&gt; method of &lt;code&gt;http&lt;/code&gt; module to create a server.&lt;/li&gt;
&lt;li&gt;Whenever a new request is received, the &lt;code&gt;request&lt;/code&gt; event is called, providing two objects: an http.IncomingMessage object and an http.ServerResponse object.&lt;/li&gt;
&lt;li&gt;The created server listens to the host and port.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's it. Now, running &lt;code&gt;node server.js&lt;/code&gt;, where &lt;code&gt;server.js&lt;/code&gt; is the name of your code file, will make your NodeJS server "up and running."&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
