{"componentChunkName":"component---src-templates-posts-js","path":"/blogs/page/4","result":{"data":{"allContentfulPost":{"edges":[{"node":{"slug":"choosing-the-right-node-js-framework-next-nuxt-nest","title":"Choosing the right Node.js Framework: Next, Nuxt, Nest?","image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/2Zac9uxugtE8AunstNN6ip/cbc4b669fc07fb954bc870f756f08dfc/24-min.jpg","fileName":"24-min.jpg"}},"bodyContent":{"childMarkdownRemark":{"html":"<p>In my previous blog post, I explored the differences, advantages, and disadvantages of three of the most popular Node.js frameworks: <a href=\"https://nodesource.com/blog/Express-Koa-Hapi\">Express, Koa, and Hapi</a>. In this blog post, I’m going to examine the differences between three more very popular frameworks: Next, Nuxt, and Nest. These three frameworks are server-side rendering, and they are closely related to React, Vue, and Angular (the three most widely used front-end frameworks), respectively.</p>\n<ul>\n<li>\n<p>The comparison is based on:</p>\n<ul>\n<li>Popularity (GitHub Stars and npm downloads).</li>\n<li>Installation.</li>\n<li>Basic Hello World app.</li>\n<li>Advantages.</li>\n<li>Disadvantages.</li>\n<li>Performance.</li>\n<li>Community involvement.</li>\n</ul>\n</li>\n</ul>\n<h1>Next</h1>\n<ul>\n<li><a href=\"https://github.com/zeit/next.js/\">GitHub Stars: +36,000</a></li>\n<li><a href=\"https://www.npmjs.com/package/next\">npm weekly downloads: +300,000</a></li>\n</ul>\n<p>Next is the most popular framework compared to the other two. It has more npm weekly downloads, GitHub stars and number of contributors.</p>\n<p>Next.js is a React framework that lets you build server-side rendering and static web applications using React.</p>\n<h3>Installation</h3>\n<p>Install it:</p>\n<pre><code>npm install --save next react react-dom\n</code></pre>\n<p>and add a script to your package.json like this:</p>\n<pre><code>{\n  \"scripts\": {\n    \"dev\": \"next\",\n    \"build\": \"next build\",\n    \"start\": \"next start\"\n  }\n}\n</code></pre>\n<p>After that, the file-system is the main API. Every .js file becomes a route that gets automatically processed and rendered.</p>\n<h3>Basic Hello World app</h3>\n<p>Populate <code>./pages/index.js</code> inside your project:</p>\n<pre><code>function Home() {\n  return &#x3C;div>Hello world!&#x3C;/div>;\n}\n\nexport default Home;\n</code></pre>\n<p>Then just run <code>npm run dev</code> and go to <code>http://localhost:3000</code>. To use another port, you can run <code>npm run dev -- -p &#x3C;your port here></code>.</p>\n<h3>Advantages</h3>\n<ul>\n<li>Every component is server-rendered by default</li>\n<li>Automatic code splitting for faster page loads </li>\n<li>Unnecessary code is not loaded</li>\n<li>Simple client-side routing (page-based)</li>\n<li>Webpack-based dev environment which supports Hot Module Replacement (HMR)</li>\n<li>Fetching data is very simple</li>\n<li>Can be implemented with Express or any other Node.js HTTP server</li>\n<li>It’s possible to customize with your own Babel and Webpack configurations</li>\n<li>Easy to deploy anywhere if Node.js is supported</li>\n<li>Built-in handling of search engine optimization (SEO) for pages </li>\n</ul>\n<h3>Disadvantages</h3>\n<ul>\n<li>Next.js is not backend; if you need backend logic, such as a database or an accounts server, you should keep that in a separate server application</li>\n<li>Next is powerful, but If you’re creating a simple app, it can be overkill</li>\n<li>All data needs to be loadable from both the client and server</li>\n<li>Migrating a server-side app to Next.js is not a quick process, and depending on your project it may be too much work</li>\n</ul>\n<h3>Performance</h3>\n<p>To measure the performance, I used <a href=\"https://httpd.apache.org/docs/2.4/programs/ab.html\">Apache Bench</a> for benchmarking, which highlights how many requests per second the app is capable of serving.  I also used <a href=\"https://developers.google.com/web/tools/lighthouse/\">lighthouse</a> to audit performance, accessibility, best practices, and SEO.</p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/5gcP7k5WIw7h4mSYuZIDMq/898ad62724d65c969e60d305e1e85d2e/next.png\" alt=\"next\"></p>\n<p>This is a basic Hello World app in Next.js. It handles 550.87 requests per second. This value is the result of dividing the number of requests by the total time taken. The average time spent per request is 18.153 ms. </p>\n<p>Compared to the other two frameworks, Next.js scored better overall than Nuxt.js but worse than Nest.js</p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/6SWp0mED1Nb9fXIBM7WwXC/4982654aa94d5d89fc6a0053b1ed90d1/next1.png\" alt=\"next1\"></p>\n<p>In the report provided by lighthouse, we can see that the performance, accessibility, best practices, and SEO scores are all above 70, which is good, but compared with the other two frameworks, it had the lowest score for  Performance and has the highest score in Best Practices. </p>\n<h3>Community involvement</h3>\n<ul>\n<li><a href=\"https://github.com/zeit/next.js/graphs/contributors\">Number of contributors: 678</a></li>\n<li><a href=\"https://github.com/zeit/next.js/pulls\">Closed Pull Requests: 3,029</a></li>\n</ul>\n<p>The Next.js community communicates through <a href=\"https://spectrum.chat/next-js?tab=posts\">chat</a>, <a href=\"https://zeit.co/chat\">slack</a>, issues and pull request on <a href=\"https://github.com/zeit/next.js\">GitHub</a>.</p>\n<p>Also, in the repo <a href=\"https://github.com/unicodeveloper/awesome-nextjs\">awesome-nextjs</a>, there is a list of essentials, articles, boilerplates, extensions, apps, books, and videos that are useful for developers using Next.js</p>\n<h1>Nuxt</h1>\n<ul>\n<li><a href=\"https://github.com/nuxt/nuxt.js\">GitHub Stars: +19,000</a></li>\n<li><a href=\"https://www.npmjs.com/package/nuxt\">npm weekly downloads: +100,000</a></li>\n</ul>\n<p>Nuxt is a Vue.js Meta Framework to create complex, fast, and universal web applications quickly.</p>\n<h3>Installation</h3>\n<p>Install it:</p>\n<pre><code>$ npm i nuxt\n</code></pre>\n<p>To create a basic app:</p>\n<pre><code>$ npx create-nuxt-app &#x3C;project-name>\n</code></pre>\n<p>You can start directly with the CLI <a href=\"https://github.com/nuxt-community/create-nuxt-app\">create-nuxt-app</a> for the latest updates.\nOr you can start by using one of the starter templates:\n<a href=\"https://github.com/nuxt-community/starter-template\">starter</a>: Basic Nuxt.js project template\n<a href=\"https://github.com/nuxt-community/express-template\">express</a>: Nuxt.js + Express\n<a href=\"https://github.com/nuxt-community/koa-template\">koa</a>: Nuxt.js + Koa\n<a href=\"https://github.com/nuxt-community/adonuxt-template\">adonuxt</a>: Nuxt.js + AdonisJS\n<a href=\"https://github.com/nuxt-community/micro-template\">micro</a>: Nuxt.js + Micro\n<a href=\"https://github.com/nuxt-community/nuxtent-template\">nuxtent</a>: Nuxt.js + Nuxtent module for content heavy sites</p>\n<h3>Basic Hello World app</h3>\n<p>This is the most basic example of a <a href=\"https://nuxtjs.org/examples/\">“Hello World!”</a> app on Nuxt:</p>\n<pre><code>&#x3C;template>\n  &#x3C;div>\n    &#x3C;h1>Hello world!&#x3C;/h1>\n    &#x3C;NLink to=\"/about\">\n      About Page\n    &#x3C;/NLink>\n  &#x3C;/div>\n&#x3C;/template>\n\n&#x3C;script>\nexport default {\n  head: {\n    title: 'Home page'\n  }\n}\n&#x3C;/script>\n</code></pre>\n<h3><a href=\"https://medium.com/vue-mastery/10-reasons-to-use-nuxt-js-for-your-next-web-application-522397c9366b\">Advantages</a></h3>\n<ul>\n<li>Its main scope is UI rendering, while abstracting away the client/server distribution</li>\n<li>Statically render your Vue apps and get all of the benefits of a universal app without a server</li>\n<li>Get automatic code splitting (pre-rendered pages)</li>\n<li>Setup via the command line with the starter template</li>\n<li>Get great project structure by default</li>\n<li>Easily set up transitions between your routes and write single file components</li>\n<li>Get ES6/ES7 compilation without any extra work</li>\n<li>Get set up with an auto-updating server for easy development</li>\n<li>Powerful Routing System with Asynchronous Data</li>\n<li>Static File Serving</li>\n<li>ES6/ES7 Transpilation</li>\n<li>Hot module replacement in Development</li>\n<li>Pre-processor: Sass, Less, Stylus, etc.</li>\n</ul>\n<h3>Disadvantages</h3>\n<ul>\n<li>It has a smaller community, which means fewer resources and potentially less extensive documentation</li>\n<li>Lack of some common solid plugins/components. (Google maps, calendar, vector maps). Some components for that exist, but they are generally not very well maintained.</li>\n<li>It is necessary to go deep in more complex components/plugins. If you want to develop something very flexible, you have to get down to render functions/jsx to do that. (e.g render the contents of a slot in another place/component).</li>\n<li>Props have to be specified explicitly. There might be cases when you want to transform some CSS classes to props;  you’ll have to specify these props or use $attrs / render functions or jsx.</li>\n<li>Reactivity caveats like setting an item from an array directly <code>this.items[key]=value</code> or adding a new data property. </li>\n<li>High traffic may put strain on your server</li>\n<li>You can only query and manipulate the DOM in certain hooks</li>\n</ul>\n<h3>Performance</h3>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/77aAB8VVmQi8Kif9gTCQ4U/2fd522c337487dce659994e37e8edbeb/nuxt.png\" alt=\"nuxt\"></p>\n<p>This is a basic Hello World app in Nuxt.js. It handles 190.05 requests per second. The average time spent per request is 52.619 ms. On this metric, Nuxt.js performs the worst compared to the other two frameworks.</p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/7hS5nyqzb8UAFd9NidP5sQ/e0cf7d7936142535c0c8aedaad038d81/nuxt1.png\" alt=\"nuxt1\"></p>\n<p>Nuxt.js has the highest score in three of the four measures; performance, accesibility and SEO. </p>\n<h3>Community involvement</h3>\n<ul>\n<li><a href=\"https://github.com/nuxt/nuxt.js/graphs/contributors\">Number of contributors: 191</a> </li>\n<li><a href=\"https://github.com/nuxt/nuxt.js/pulls\">Closed Pull Requests: 1,385</a></li>\n</ul>\n<p>There is a <a href=\"https://github.com/nuxt-community\">GitHub organization</a> where you can find modules and projects from the Nuxt.js community. There is also a curated list of awesome things related to Nuxt.js <a href=\"https://github.com/nuxt-community/awesome-nuxt\">awesome-nuxt</a> including Modules, tools, mention of Nuxt.js, showcase, tutorials, blogs, books, starter template, official examples, and projects using Nuxt.js.</p>\n<p>The community communicates through <a href=\"https://gitter.im/nuxt/nuxt.js\">Gitter Chat Room</a>, <a href=\"https://t.me/nuxtjs_ru\">Telegram, Russian community</a>, <a href=\"https://discordapp.com/invite/VApZF5W\">Discord</a>, <a href=\"https://twitter.com/nuxt_js\">Twitter</a> and <a href=\"https://www.youtube.com/channel/UCJ9jj5YMzo-HsyM6WG9Q_Lg\">YouTube Channel</a> </p>\n<h1>Nest</h1>\n<ul>\n<li><a href=\"https://github.com/nestjs/nest\">GitHub Stars: +14,000</a></li>\n<li><a href=\"https://www.npmjs.com/package/@nestjs/core\">npm weekly downloads: +122,000</a></li>\n</ul>\n<p>A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications on top of TypeScript and JavaScript (ES6, ES7, ES8), Nest is heavily inspired by Angular. </p>\n<p>Nest is a framework for building efficient, scalable Node.js server-side applications. It uses modern JavaScript, is built with TypeScript (preserves compatibility with pure JavaScript) and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Reactive Programming).</p>\n<p>Under the hood, Nest makes use of Express, but also provides compatibility with a wide range of other libraries, like e.g. Fastify, allowing for easy use of the myriad third-party plugins which are available.</p>\n<h3>Installation</h3>\n<p>Install it:</p>\n<pre><code>$ npm i @nestjs/cli\n$ nest new project-name\n</code></pre>\n<p>Alternatively, to install the TypeScript starter project with Git:</p>\n<pre><code>$ git clone https://github.com/nestjs/typescript-starter.git project\n$ cd project\n$ npm install\n$ npm run start\n</code></pre>\n<h3>Basic Hello World app</h3>\n<p>After installing Nest.js with the <code>npm cli</code> command, and creating a new project with <code>nest new project-name</code>, a <code>src/</code> directory will be created and populated with several core files, including <code>main.ts</code>.\nThe <code>main.ts</code> includes an async function, which will bootstrap our application:</p>\n<pre><code>import { NestFactory } from '@nestjs/core';\nimport { ApplicationModule } from './app.module';\n\nasync function bootstrap() {\n  const app = await NestFactory.create(ApplicationModule);\n  await app.listen(3000);\n}\nbootstrap();\n</code></pre>\n<p>And then to run the app that listens on port 3000, you execute: </p>\n<pre><code>$ npm run start\n</code></pre>\n<h3><a href=\"https://medium.com/@jtearl188/what-is-nest-js-and-should-i-use-it-b71c7646926b\">Advantages</a></h3>\n<ul>\n<li>As a TypeScript-based web framework, strict type definition is possible</li>\n<li>The framework is very annotation-driven, with everything from endpoints to Swagger documentation being generated from them. The endpoints are clean and simple, and the annotations make developing simpler all around.</li>\n<li>The folder structure in Nest.js is heavily based on Angular. This allows for minimal downtime when first designing a Nest service.</li>\n<li>Because Nest.js is a module-based framework, it’s easyto externalize general-purpose modules and reuse code in multiple projects</li>\n<li>Components get their own folders, with an application module and main file residing in the root. This simple structure allows more attention to be paid to the design of endpoints and their consumers, instead of application structure.</li>\n<li>Nest.js uses the latest version of TypeScript, which helps ensure that it will remain relevant in the rapidly changing JavaScript landscape and gives developers less context switching. The transition from Angular code to Nest is relatively easy.</li>\n<li>Similar to Angular, Nest also has a decent command line tool, available through Node Package Manager, nestjs/cli. The command line tool will let you scaffold the project, generate Nest architecture components, and display project information.</li>\n</ul>\n<h3>Disadvantages</h3>\n<ul>\n<li>The largest risk facing Nest users is the lack of documentation. The framework has great integrations with other frameworks but the documentation is minimal and doesn’t cover any issues that may arise.</li>\n<li>Nest does hold an edge in its use of TypeScript and relation to Angular, but it doesn’t have the backing power of a large enterprise behind it. </li>\n<li>Overall, Nest has a smaller community compared to other frameworks</li>\n</ul>\n<h3>Performance</h3>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/1I29Vhrd0whIOGhQsGPMUT/8092e2e37a828c4cad9f70628854f378/nest.png\" alt=\"nest\"></p>\n<p>This is a basic Hello World app in Nest.js. It handles 928.18 requests per second. The average time spent per request is 10.774 ms. On this metric, Nest.js performed the best out of the three frameworks we compared. </p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/4bL41CK2YoIadG76nKqbgp/a4dd5d419adfee21dbfa09d8b7088c61/nest2.png\" alt=\"nest2\"></p>\n<p>In the report provided by lighthouse, Nest.js has a very high performance, but scored comparatively lower on other key factors:accessibility, best practices and SEO.</p>\n<h3>Community involvement</h3>\n<ul>\n<li><a href=\"https://github.com/nestjs/nest/graphs/contributors\">Number of contributors: 81</a>  </li>\n<li><a href=\"https://github.com/nestjs/nest/pulls\">Closed Pull Requests: 469</a> </li>\n</ul>\n<p>There is a group of developers providing handy packages on <a href=\"https://github.com/nestjs-community\">NestJS Community organization GitHub</a>. Some of their popular packages are: <a href=\"https://github.com/nestjs-community/nestjs-config\">nestjs-config</a>, a config module for NestJS using <code>dotenv</code>. <a href=\"https://github.com/nestjs-community/nest-access-control\">nest-access-control</a>, Role and Attribute-based access control for NestJS and <a href=\"https://github.com/nestjs-community/nestjs-flub\">nestjs-flub</a>, pretty error stack viewer.</p>\n<p>Even if Nest is not the most popular framework, is the one that has the better performance and has many advantages. You should give it a try! </p>\n<p>The community has a <a href=\"https://spectrum.chat/nestjs?tab=posts\">spectrum chat</a> and <a href=\"https://twitter.com/nestframework?lang=en\">Twitter</a></p>"}},"categories":[{"name":"Node.js","slug":"node-js"},{"name":"Performance","slug":"performance"}]}},{"node":{"slug":"understanding-worker-threads-in-node-js","title":"Understanding Worker Threads in Node.js","image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/1v4gDfjjlafQJlKOnwWG3l/f4599196d0a510d6fa0c83893edb3905/23-min.png","fileName":"23-min.png"}},"bodyContent":{"childMarkdownRemark":{"html":"<p>To understand Workers, first, it’s necessary to understand how Node.js is structured. </p>\n<p>When a Node.js process is launched, it runs:</p>\n<ul>\n<li>One process</li>\n<li>One thread</li>\n<li>One event loop</li>\n<li>One JS Engine Instance</li>\n<li>One Node.js Instance</li>\n</ul>\n<p><strong>One process:</strong> a process is a global object that can be accessed anywhere and has information about what’s being executed at a time.</p>\n<p><strong>One thread:</strong> being single-threaded means that only one set of instructions is executed at a time in a given process.</p>\n<p><strong>One event loop:</strong> this is one of the most important aspects to understand about Node. It’s what allows Node to be asynchronous and have non-blocking I/O, — despite the fact that JavaScript is single-threaded — by offloading operations to the system kernel whenever possible through callbacks, promises and async/await.</p>\n<p><strong>One JS Engine Instance:</strong> this is a computer program that executes JavaScript code.</p>\n<p><strong>One Node.js Instance:</strong> the computer program that executes Node.js code.</p>\n<p>In other words, Node runs on a single thread, and there is just one process happening at a time in the event loop. One code, one execution, (the code is not executed in parallel). This is very useful because it simplifies how you use JavaScript without worrying about concurrency issues. </p>\n<p>The reason it was built with that approach is that JavaScript was initially created for client-side interactions (like web page interactions, or form validation) -- nothing that required the complexity of multithreading.</p>\n<p>But, as with all things, there is a downside: if you have CPU-intensive code, like complex calculations in a large dataset taking place in-memory, it can block other processes from being executed. Similarly, If you are making a request to a server that has CPU-intensive code, that code can block the event loop and prevent other requests of being handled.</p>\n<p>A function is considered “blocking” if the main event loop must wait until it has finished executing the next command. A “Non-blocking” function will allow the main event loop to continue as soon as it begins and typically alerts the main loop once it has finished by calling a “callback”.</p>\n<blockquote>\n<p><strong>The golden rule: don’t block the event loop,</strong> try to keep it running it and pay attention and avoid anything that could block the thread like synchronous network calls or infinite loops.</p>\n</blockquote>\n<p>It’s important to differentiate between CPU operations and I/O (input/output) operations. As mentioned earlier, the code of Node.js is NOT executed in parallel. <strong>Only I/O operations are run in parallel</strong>, because they are executed asynchronously.</p>\n<p>So Worker Threads will not help much with I/O-intensive work because asynchronous I/O operations are more efficient than Workers can be. The main goal of Workers is to improve the performance on CPU-intensive operations not I/O operations.</p>\n<h3>Some solutions</h3>\n<p>Furthermore, there are already solutions for CPU intensive operations: multiple processes (like cluster API) that make sure that the CPU is optimally used. </p>\n<p>This approach is advantageous because it allows isolation of processes, so if something goes wrong in one process, it doesn’t affect the others. They also have stability and identical APIs. However, this means sacrificing shared memory, and the communication of data must be via JSON.</p>\n<h3>JavaScript and Node.js will never have threads, this is why:</h3>\n<p>So, people might think that adding a new module in Node.js core will allow us to create and sync threads, thus solving the problem of CPU-intensive operations.</p>\n<p>Well, no, not really. If threads are added, the nature of the language itself will change. It’s not possible to add threads as a new set of available classes or functions. In languages that support multithreading (like Java), keywords such as “synchronized” help to enable multiple threads to sync. </p>\n<p>Also, some numeric types are not atomic, meaning that if you don’t synchronize them, you could end up having two threads changing the value of a variable and resulting that after both threads have accessed it, the variable has a few bytes changed by one thread and a few bytes changed by the other thread and thus, not resulting in any valid value. For example, in the simple operation of 0.1 + 0.2 has 17 decimals in JavaScript (the maximum number of decimals).</p>\n<p><code>var x = 0.1 + 0.2; // x will be 0.30000000000000004</code></p>\n<p>But floating point arithmetic is not always 100% accurate. So if not synchronized, one decimal may get changed using Workers, resulting in non-identical numbers. </p>\n<h3>The best solution:</h3>\n<p>The best solution for CPU performance is Worker Threads.  Browsers have had the concept of Workers for a long time.</p>\n<p>Instead of having:  </p>\n<ul>\n<li>One process</li>\n<li>One thread</li>\n<li>One event loop</li>\n<li>One JS Engine Instance</li>\n<li>One Node.js Instance</li>\n</ul>\n<p>Worker threads have:</p>\n<ul>\n<li>One process</li>\n<li><em>Multiple</em> threads</li>\n<li>One event loop <em>per thread</em></li>\n<li>One JS Engine Instance <em>per thread</em></li>\n<li>One Node.js Instance <em>per thread</em></li>\n</ul>\n<p>As we can see in the following image:</p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/20h5efXHT4bQbuf44mdq2H/a40944191d031217a9169b17a8ef35d6/worker-diagram_2x__1_.jpg\" alt=\"worker-diagram@2x (1)\"></p>\n<p>The <code>worker_threads</code> module enables the use of threads that execute JavaScript in parallel. To access it:</p>\n<pre><code>const worker = require('worker_threads');\n</code></pre>\n<p>Worker Threads have been available since Node.js 10, but are still in the experimental phase.</p>\n<div class=\"blog-cta nsolid\">\n    Get started with low-impact performance monitoring\n  <a class=\"button more large\" href=\"https://accounts.nodesource.com/sign-up-lambda-blogref-m\">Create your NodeSource Account</a>\n</div>\n<p>What is ideal, is to have multiple Node.js instances inside the same process. With Worker threads, a thread can end at some point and it’s not necessarily the end of the parent process. It’s not a good practice for resources that were allocated by a Worker to hang around when the Worker is gone-- that’s a memory leak, and we don’t want that. We want to embed Node.js into itself, give Node.js the ability to create a new thread and then create a new Node.js instance inside that thread; essentially running independent threads inside the same process.</p>\n<p>What makes Worker Threads special:</p>\n<ul>\n<li><code>ArrayBuffers</code> to transfer memory from one thread to another</li>\n<li><code>SharedArrayBuffer</code> that will be accessible from either thread. It lets you share memory between threads (limited to binary data).</li>\n<li><code>Atomics</code> available, it lets you do some processes concurrently, more efficiently and allows you to implement conditions variables in JavaScript</li>\n<li><code>MessagePort</code>, used for communicating between different threads. It can be used to transfer structured data, memory regions and other MessagePorts between different Workers.</li>\n<li><code>MessageChannel</code> represents an asynchronous, two-way communications channel used for communicating between different threads.</li>\n<li><code>WorkerData</code> is used to pass startup data. An arbitrary JavaScript value that contains a clone of the data passed to this thread’s Worker constructor. The data is cloned as if using <code>postMessage()</code></li>\n</ul>\n<p>API</p>\n<ul>\n<li><code>const { worker, parentPort } = require(‘worker_threads’)</code> => The <code>worker</code> class represents an independent JavaScript execution thread and the <code>parentPort</code> is an instance of the message port</li>\n<li><code>new Worker(filename)</code> or <code>new Worker(code, { eval: true })</code> => are the two main ways of starting a worker (passing the filename or the code that you want to execute). It’s advisable to use the filename in production.</li>\n<li><code>worker.on(‘message’)</code>, <code>worker/postMessage(data)</code> => for listening to messages and sending them between the different threads. </li>\n<li><code>parentPort.on(‘message’)</code>, <code>parentPort.postMessage(data)</code> => Messages sent using <code>parentPort.postMessage()</code> will be available in the parent thread using <code>worker.on('message')</code>, and messages sent from the parent thread using <code>worker.postMessage()</code> will be available in this thread using <code>parentPort.on('message')</code>.</li>\n</ul>\n<h3>EXAMPLE:</h3>\n<pre><code>const { Worker } = require('worker_threads');\n\nconst worker = new Worker(`\nconst { parentPort } = require('worker_threads');\nparentPort.once('message',\n    message => parentPort.postMessage({ pong: message }));  \n`, { eval: true });\nworker.on('message', message => console.log(message));      \nworker.postMessage('ping');  \n</code></pre>\n<pre><code>$ node --experimental-worker test.js\n{ pong: ‘ping’ }\n</code></pre>\n<center><figcaption>Example by Anna Henningsen</figcaption></center>\n<p>What this essentially does is create a new thread using a new Worker, the code inside the Worker is listening for a message on <code>parentPort</code> and once it receives the message, it is going to post the message back to the main thread.</p>\n<p>You have to use the <code>--experimental-worker</code> because Workers are still experimental.</p>\n<p>Another example:</p>\n<pre><code>    const {\n      Worker, isMainThread, parentPort, workerData\n    } = require('worker_threads');\n\n    if (isMainThread) {\n      module.exports = function parseJSAsync(script) {\n        return new Promise((resolve, reject) => {\n          const worker = new Worker(filename, {\n            workerData: script\n          });\n          worker.on('message', resolve);\n          worker.on('error', reject);\n          worker.on('exit', (code) => {\n            if (code !== 0)\n              reject(new Error(`Worker stopped with exit code ${code}`));\n          });\n        });\n      };\n    } else {\n      const { parse } = require('some-js-parsing-library');\n      const script = workerData;\n      parentPort.postMessage(parse(script));\n    }\n</code></pre>\n<p>It requires:</p>\n<ul>\n<li><code>Worker</code>: the class that represents an independent JavaScript execution thread.</li>\n<li><code>isMainThread</code>: a boolean that is true if the code is not running inside of a Worker thread.</li>\n<li><code>parentPort</code>: the MessagePort allowing communication with the parent thread If this thread was spawned as a Worker.</li>\n<li><code>workerData</code>: An arbitrary JavaScript value that contains a clone of the data passed to this thread’s Worker constructor.</li>\n</ul>\n<p>In actual practice for these kinds of tasks, use a pool of Workers instead. Otherwise, the overhead of creating Workers would likely exceed their benefit.</p>\n<h3>What is expected for Workers (hopefully):</h3>\n<ul>\n<li>Passing native handles around (e.g. sockets, http request) </li>\n<li>Deadlock detection. Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process. Deadlock detention will be useful for Worker threads in this case. </li>\n<li>More isolation, so if one process is affected, it won’t affect others.</li>\n</ul>\n<h3>What NOT to expect for Workers:</h3>\n<ul>\n<li>Don’t think Workers make everything magically faster, in some cases is better to use Worker pool</li>\n<li>Don’t use Workers for parallelizing I/O operations.</li>\n<li>Don’t think spawning Workers is cheap</li>\n</ul>\n<h2>Final notes:</h2>\n<p>The contributors to Workers in Node.js are looking for feedback, if you have used Workers before and want to contribute, you can leave your feedback <a href=\"https://github.com/nodejs/worker/issues/6\">here</a></p>\n<p>Workers have chrome DevTools support to inspect Workers in Node.js.</p>\n<p>And <code>worker_threads</code> is a promising experimental module if you need to do CPU-intensive tasks in your Node.js application. Keep in mind that it’s still experimental, so it is advisable to wait before using it in production. For now, you can use Worker pools instead.</p>\n<h4>References:</h4>\n<p>Special thanks to <a href=\"https://twitter.com/addaleax\">Anna Henningsen</a> and her amazing talk of <a href=\"https://www.youtube.com/watch?v=-ssCzHoUI7M\">Node.js: The Road to Workers</a></p>\n<p><a href=\"https://nodejs.org/api/worker_threads.html#worker_threads_worker_threads\">Node.js API</a></p>\n<p><a href=\"https://blog.logrocket.com/node-js-multithreading-what-are-worker-threads-and-why-do-they-matter-48ab102f8b10/\">Node.js multithreading: What are Worker Threads and why do they matter?</a> - by <a href=\"https://twitter.com/gimenete\">Alberto Gimeno</a></p>\n<p><a href=\"https://quickleft.com/blog/introduction-to-javascript-processes/\">Introduction to Javascript Processes</a> - by <a href=\"https://quickleft.com/blog/author/nico/\">Nico Valencia</a></p>\n<p><a href=\"https://flaviocopes.com/node-event-loop/\">The Node.js Event Loop</a></p>"}},"categories":[{"name":"Tutorials","slug":"tutorials"},{"name":"Node.js","slug":"node-js"}]}},{"node":{"slug":"microservices-in-node-js","title":"Explain like I'm 5: Microservices in Node.js","image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/6yr8kp0ySnhTFiFp61SpSc/4966628ebe58c7b42a913b387547ff29/22-min.png","fileName":"22-min.png"}},"bodyContent":{"childMarkdownRemark":{"html":"<p>Microservices are an architectural approach based on building an application as a collection of small services.</p>\n<p>Let’s think of an application as a store. Applications are traditionally “monolithic” which means they are built as a single, autonomous unit --think of your favorite big-box store, selling everything from bananas to lawn furniture. </p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/77vR95BkR7cPdBpk8GU9qN/08f2319787496a113693f4a76a8d06a9/lego.png\" alt=\"lego\"></p>\n<p>Everything is contained inside the unit. Let’s imagine that the person in this image - I’ll call him Jeff - is going inside the store to buy chewing gum and t-shirts. He can get a shopping cart to carry his purchases, look at products in different aisles, and pay at the checkstand before leaving--essentially, everything he needs is inside the store. These could also just as easily be components of an online Lego store application 👇.</p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/4DGmPnumf0bVfLZiOAVtEF/5c201fe9423d10b4f1d34b94c011cf8c/lego-store.png\" alt=\"lego-store\"> </p>\n<p>All of this is run within a single process, and if our Lego store becomes very popular and we want to expand the business, we will have to add more Lego blocks in the same unit... and in the case of the online store, add more servers in order to scale it out.</p>\n<p>So, every change (even minor changes) in our Lego store (physical or online) can be slow or tedious as it affects the entire system.  In the case of Jeff’s store, a modification can require the removal or addition of multiple Lego blocks, affecting the entire structure. In the monolithic online store, a modification made to a small section of code might require building and deploying an entirely new version of software. So, scaling specific functions or components of the application, also means you have to scale the entire system.</p>\n<p>Other problems with a monolithic approach in an online application are:</p>\n<ul>\n<li><strong>Inflexibility:</strong> it cannot be built using different technologies</li>\n<li><strong>Potentially unreliable:</strong> if even one feature of the system does not work, then the entire system does not work</li>\n<li><strong>Unscalable:</strong> applications cannot be scaled easily, since each time the application needs to be updated, the complete system has to be rebuilt</li>\n<li><strong>Not suitable for continuous development:</strong> many features of an application cannot be built and deployed at the same time</li>\n<li><strong>Slow development:</strong> As you can likely guess from the preceding points, development in monolithic applications takes a lot of time, since each feature has to be built individually, one after the other, rather than allowing multiple features to be worked on concurrently</li>\n</ul>\n<p>This is where microservices come to the rescue! </p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/KCURMUDObC5hrpczE8bxQ/67cc4ad8b0cf96137fe39d5b679eb4b2/microservices.jpg\" alt=\"microservices\"></p>\n<p>Instead of containing everything in a single unit, the microservices-based application is broken down into smaller, lightweight pieces based on a logical construct. The application consists of independent small (micro-) services,  and when we deploy or scale the app, individual services get distributed within a set of machines which we call “a cluster” in the service fabric world.</p>\n<p>So in our Lego store example, perhaps one microservice contains a shopping cart, another one a product catalog, while another handles checkout, and so on. This approach allows developers to  embrace compact and specialized tools that get each job done properly. Microservices are exactly that, scaled to enterprise level.</p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/6szXHVXaOaCxZD2ORnZ0FY/73f0df8b11694cc3274d9483a99e7f11/Microservices_Diagram.jpg\" alt=\"Microservices Diagram\"></p>\n<p>Each service has its own unique and well-defined role, runs in its own process, and communicates via HTTP APIs or messaging. Each microservice can be deployed, upgraded, scaled, and restarted independently of all the sibling services in the application. They are typically managed by an automated system, making it possible to deploy frequent updates to live applications without affecting the end-users.</p>\n<p>Following this pattern, Jeff’s store will be very different: now he won’t have one big store where he can find everything he needs, but there would have multiple stores and each store will be independent and have specific functions. The first store may contain only Lego castles, another one bridges, and another one, Lego rockets 🚀. </p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/1LV7Wy8f7xhgltfsFgKtG7/4c437b8b0bb1384bcd7836bd439e0f5e/Screen_Shot_2019-08-08_at_4.36.33_PM.png\" alt=\"Screen Shot 2019-08-08 at 4.36.33 PM\"></p>\n<p>All of the Lego stores will be part of a “Lego shopping mall” or “cluster,” and if I want to expand, scale, upgrade, or modify only the store selling rockets, the castle store (and the rest) won’t be affected.</p>\n<p>In other words, developers identify the separate service “pieces” that are logically closely related and necessary parts of a project. Then, they choose from the options available that meet their particular needs, from open source to enterprise solutions, and knit everything together into a functional application.</p>\n<h3>Advantages of using microservices:</h3>\n<ul>\n<li>Allows us to build, operate and manage services independently, and we can easily scale them out based on the resources they need.</li>\n<li>Microservices take a lot of infrastructure risk out of the project straight away. With the infrastructure made almost invisible, microservice teams can iterate quickly.</li>\n<li>Each developer on a team can avoid getting tangled up in the underlying infrastructure, and focus on their piece of the project. Then, in production, if individual project modules don’t work exactly right together, it’s easy enough to isolate, disassemble and reconfigure them until they do. If shoppers aren’t big fans of the mall’s specialty ketchup store, a shoe store can be built in its place. It offers better resource utilization and cost optimization</li>\n<li>Microservices have their own load balancer and execution environment to execute their functionalities, and at the same time, capture data in their own databases.</li>\n<li>Finally, microservices offer language and platform freedom, so teams can choose the best language for the job at hand (even if that’s .NET for one team and Node.js for another team).</li>\n</ul>\n<h3>Drawbacks of microservices:</h3>\n<ul>\n<li>Microservices are not automatically the right solution for every project. When you are running multiple instances of the same service or worker, you don’t necessarily need microservices. A well-built monolithic system can scale just as well for some classes of problems.</li>\n<li>One of the big problems with microservices is “orchestration”, which means how to integrate the services with a guide to drive the process, much like a conductor in an orchestra. Integrating microservices can be quite complex.</li>\n<li>Another complex process is “discovery” which is how applications and (micro)services locate each other on a network. </li>\n<li>Moving away from a monolithic app architecture means the loss of an opinionated workflow that previously glued all the pieces together.</li>\n<li>There is a risk in getting a very fragmented system where developers need to spend a lot of time and effort on gluing together services and tools, and where there’s a lack of common patterns and platforms that makes it difficult to work across different projects.  </li>\n<li>Microservices can also require increased testing complexity and possibly increased memory/computing resources.</li>\n<li>It’s possible to create un-scalable microservices. It all comes down to how well you apply the fundamental principles. It’s all too easy to jump into shopping for all the microservices you want to apply without first truly considering the problem set you’re applying them to</li>\n</ul>\n<h2>Creating Microservices with Node.js</h2>\n<p>In this example, we’re going to create a microservice using Node.js which connects to an external API.</p>\n<p>The requirement for this service is to accept two Zip Codes of two Lego stores and return the distance between them in miles. </p>\n<h3>Initial Steps</h3>\n<ol>\n<li>Have Node.js installed </li>\n<li>Run <code>npm init</code> in the root folder for the project. This will create a package.json file that will prompt some questions about the package, if you are not sure how to answer you can use the default.</li>\n<li>We are going to use two packages, Express and Require that can be installed like this: </li>\n</ol>\n<pre><code>$ npm install express request --save\n</code></pre>\n<p>Let’s look at the structure of the folders. There are two files and a folder created by the npm init command. These are <code>package.json</code>, <code>package-lock.json</code>, and <code>node_modules</code>. When we installed the express and request packages, their dependencies were downloaded and saved in <code>node_modules</code>.</p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/669RoiBZTvj67W7QNHccrl/2d7fd15349bd9e7f0056b27e3f5a0e68/Screen_Shot_2019-08-01_at_3.54.03_PM.png\" alt=\"Screen Shot 2019-08-01 at 3.54.03 PM\"></p>\n<p>The primary file in our project is named server.js. And your package.json should look similar to this ☝️.</p>\n<p>Then we create two folders, <code>api</code> for files that will support the API, and <code>service</code> for the logic to connect to a third-party API.</p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/5aE2egdP7TrDWu1Az3stj9/eaaa5b311f14d2f28339ade15a3aee22/Screen_Shot_2019-08-01_at_3.58.17_PM.png\" alt=\"Screen Shot 2019-08-01 at 3.58.17 PM\"></p>\n<p>Let’s build our service!</p>\n<h3>Creating a Server to Accept Requests</h3>\n<p>Create a file in the root folder for your project called server.js which will be our primary file. This file contains the code below.</p>\n<pre><code class=\"language-javascript\">const express = require('express')\nconst app = express();\nconst port = process.env.PORT || 3000;\n\nconst routes = require('./api/routes');\nroutes(app);\napp.listen(port, function() {\n   console.log('Server started on port: ' + port);\n});\n</code></pre>\n<p>This file is creating our server and assigns routes to process all requests. </p>\n<p>We first require express into the file, and use it to create a new app object <code>const app = express();</code> then we specify the port, in this case, we use the environment variable called PORT, and if the variable isn’t defined, it will use the default port: 3000.</p>\n<p>Then we bring the routes object from the routes.js file in the api folder. We’ll pass the app to the routes object, and that sets the routes for our application. Finally, we’ll tell the app to start listening on the port we defined and to display a message to the console when this process is complete.</p>\n<h3>Defining the routes</h3>\n<p>The next step is to define the routes for the microservices and then assign each to a target in the controller object (that will control the flow of data in the application). We’ll build the controller in the next step. We’ll have two endpoints. One endpoint called “about” that returns information about the application. And a “distance” endpoint that includes two path parameters, both Zip Codes of the Lego store. This endpoint returns the distance, in miles, between these two Zip Codes.</p>\n<pre><code class=\"language-javascript\">'use strict';\n\nconst controller = require('./controller');\n\nmodule.exports = function(app) {\n   app.route('/about')\n       .get(controller.about);\n   app.route('/distance/:zipcode1/:zipcode2')\n       .get(controller.getDistance);\n};\n</code></pre>\n<p>The ‘use strict’ directive at the top of the file is used by new versions of Javascript to enforce secure coding practices. The first thing we’ll do is to create a controller object which we’ll define in the next step.\nModule.exports allows us to declare functions inside a module, and have them available for use in another file. This file constitutes the routes module, which we imported into our primary server.js file and used it to define the routes for our express app.\nThis function adds two routes to the app. The first route listens for GET requests on the <code>/about</code> endpoint. These requests are handled by the about function in the controller. The second route listens for GET requests on the <code>/distance</code> endpoint. The <code>getDistance</code> function in the controller handles these requests. Two parameters are also specified. These are labeled zipcode1 and zipcode2 respectively.\nLet’s build the controller to handle those requests.</p>\n<h3>Adding Controller Logic</h3>\n<p>Within the controller file, we’re going to create a controller object with two properties. Those properties are the functions to handle the requests we defined in the routes module.</p>\n<pre><code class=\"language-javascript\">'use strict';\n\nvar properties = require('../package.json')\nvar distance = require('../service/distance');\n\nvar controllers = {\n   about: function(req, res) {\n       var aboutInfo = {\n           name: properties.name,\n           version: properties.version\n       }\n       res.json(aboutInfo);\n   },\n   getDistance: function(req, res) {\n           distance.find(req, res, function(err, dist) {\n               if (err)\n                   res.send(err);\n               res.json(dist);\n           });\n       },\n};\n\nmodule.exports = controllers;\n</code></pre>\n<p>We have two distinct parts to our controller. We’ll go through the code for the about functionality first. At the top, we create an object called properties which references the package.json file which npm created when it bootstrapped the project. This file is in JavaScript Object Notation or JSON for short. This format affords us the ability to import and use the information it contains.</p>\n<p>Within the controllers object, we define a property called about. This property is a function which accepts request and response objects. We’ll only use the response object for this function. Using the name and version information from the package.json file, we’ll build a new object and return it as the response.</p>\n<p>For the getDistance functionality, we’ll start by bringing in the distance module. We’ll pass the request and response objects to the find function within this module. This function also includes a callback function. This function accepts an error object (err) and a distance object (dist). If there is an error in the response, we return that with our response; otherwise, we send back the results of the find function.</p>\n<h3>Making the External Call</h3>\n<p>We’re ready for the final piece of the puzzle. This file handles the call to a third-party API. We’ll use the distance API provided by ZipCodeAPI.com. (You need an API key to use this, and it is free if you register. You can also use the key from the example if you want to test your service, but this key frequently expires during the day).</p>\n<p>I set my key as an environment variable on my system and named it ZIPCODE<em>API</em>KEY. The default key in the code is an expired test key from the ZipCodeAPI website.</p>\n<pre><code class=\"language-javascript\">var request = require('request');\n\nconst apiKey = process.env.ZIPCODE_API_KEY || \"hkCt1nW1wF1rppaEmoor7T9G4ta7R5wFSu8l1dokNz8y53gGZHDneWWVosbEYirC\";\nconst zipCodeURL = 'https://www.zipcodeapi.com/rest/';\n\nvar distance = {\n   find: function(req, res, next) {\n       request(zipCodeURL + apiKey \n               + '/distance.json/' + req.params.zipcode1 + '/' \n               + req.params.zipcode2 + '/mile',\n       function (error, response, body) {\n           if (!error &#x26;&#x26; response.statusCode == 200) {\n               response = JSON.parse(body);\n               res.send(response);\n           } else {\n               console.log(response.statusCode + response.body);\n               res.send({distance: -1});\n           }\n       });\n\n   }\n};\n\nmodule.exports = distance;\n</code></pre>\n<p>We’re using the request package to execute the external HTTP request, and we already discussed the api Key above. Ensure that you update it unless you want to start by testing the error conditions.</p>\n<p>The find function accepts request, response and next objects as parameters. The request object accepts the URL of the service we’re calling and then defines a callback function to handle the response.</p>\n<p>If there are no errors, and the status of the response is HTTP Status code 200, then the function parses out the body of the response into an object called response and returns it on the <code>resp</code> object. Since the ZipCodeAPI returns with a JSON response, we could forward this directly. Parsing it out allows us the option of doing more with the response if we choose to.</p>\n<p>We log failures to the console, and then a result of -1 is sent on the response object. You may opt to create an error object to return as well.</p>\n<p>Finally, we export the distance object, which allows the controller to instantiate it and call its functions as needed.</p>\n<h3>Execution</h3>\n<p>Assuming there aren’t any typos, your application should be ready to execute. Open a console window and run the following command:</p>\n<pre><code>npm start\n</code></pre>\n<p>Assuming it starts correctly, and the port you define is 3000, you can now open your browser and navigate to:</p>\n<p><a href=\"http://localhost:3000/about\">http://localhost:3000/about</a> when you will see the name of the app and the version.</p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/58lstZiyoh5Pyow5UeF4RP/cb5a84facefb43e0024f2111c313cbaf/Screen_Shot_2019-08-01_at_7.17.37_PM.png\" alt=\"Screen Shot 2019-08-01 at 7.17.37 PM\"></p>\n<p>Now if you add two parameters, the two zip codes, you will see something like this:</p>\n<p><a href=\"http://localhost:3000/distance/84010/97229\">http://localhost:3000/distance/84010/97229</a></p>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/1VMI1kMNPmWNd4ip3UDolz/9c0237d15d4b1ac01375bb05a5ca5411/Screen_Shot_2019-08-01_at_7.18.55_PM.png\" alt=\"Screen Shot 2019-08-01 at 7.18.55 PM\"></p>\n<p>And that’s it! Using microservices to know the distance between two zip codes!</p>\n<h2>Conclusion</h2>\n<p>In microservices, every single service is independently deployable, scalable and updatable, this is what makes microservices such an appealing architectural approach to the industry.</p>\n<p>A microservice is loosely coupled and interacts with other microservices for well-defined interfaces using protocols like http, they remain consistent and available in the presence of failure, meaning even if the machine goes down that host a microservice, the functionality provided by the service should still be offered by the application.</p>\n<p>While microservices are great, there is quite some work involved to build a scalable microservice application on a platform as you need to consider things like cluster management, service orchestration, inter-service communication and so on, and you also need to put a lot of effort into following  DevOpsbest practices.</p>\n<p>Not to mention that microservices can also require increased testing complexity and possibly increased memory/computing resources. Thus, despite the abundant potential benefits, those knowledgeable in the field caution that microservices are not automatically the right solution for every project.</p>\n<ul>\n<li>Lego is a registered trademark</li>\n</ul>\n<h4>Resources</h4>\n<ul>\n<li><a href=\"https://www.youtube.com/watch?v=EEwT6y5h09I\">Introduction to Microservices</a></li>\n<li><a href=\"https://smartbear.com/solutions/microservices/\">What is Microservices?</a></li>\n<li><a href=\"https://thenewstack.io/microservices-101/\">Microservices 101</a></li>\n<li><a href=\"https://dzone.com/articles/what-is-microservices-an-introduction-to-microserv\">What Is Microservices? An Introduction to Microservice Architecture</a></li>\n<li><a href=\"https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/\">https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/</a></li>\n<li><a href=\"https://blog.cloud66.com/beginners-guide-to-building-real-world-microservices-with-node-js/\">Beginners Guide to Building Real-World Microservices with Node.js</a></li>\n</ul>"}},"categories":[{"name":"Tutorials","slug":"tutorials"},{"name":"Node.js","slug":"node-js"}]}},{"node":{"slug":"understanding-streams-in-node-js","title":"Understanding Streams in Node.js","image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/iF3PJ65DN4RZh7hUSRU5X/055bb44f2ab60b4efcea179638f68efe/21-min.jpg","fileName":"21-min.jpg"}},"bodyContent":{"childMarkdownRemark":{"html":"<p>Streams in Node.js have a reputation for being hard to work with, and even harder to understand. </p>\n<p>In the words of Dominic Tarr: “Streams are Node’s best and most misunderstood idea.” Even Dan Abramov, creator of Redux and core team member of React.js is afraid of Node streams.</p>\n<center>![dan](//images.ctfassets.net/hspc7zpa5cvq/6uij4d7kgJxdOy4ESjvKS5/24dc5102e9a1f2be5944a69062d0690b/dan.png)</center>\n<p>This article will help you understand streams and how to work with them. So, don’t be afraid. We can figure this out!</p>\n<h2>What are streams?</h2>\n<p>Streams are one of the fundamental concepts that power Node.js applications. They are data-handling method and are used to read or write input into output sequentially.</p>\n<p>Streams are a way to handle reading/writing files, network communications, or any kind of end-to-end information exchange in an efficient way.</p>\n<p>What makes streams unique, is that instead of a program reading a file into memory <strong>all at once</strong> like in the traditional way, streams read chunks of data piece by piece, processing its content without keeping it all in memory.</p>\n<p>This makes streams really powerful when working with <strong>large amounts of data</strong>, for example, a file size can be larger than your free memory space, making it impossible to read the whole file into the memory in order to process it. That’s where streams come to the rescue!</p>\n<p>Using streams to process smaller chunks of data, makes it possible to read larger files.</p>\n<p>Let’s take a “streaming” services such as YouTube or Netflix for example: these services don’t make you download the video and audio feed all at once. Instead,  your browser receives the video as a continuous flow of chunks, allowing the recipients to start watching and/or listening almost immediately. </p>\n<p>However, streams are not only about working with media or big data. They also give us the power of ‘composability’ in our code. Designing with composability in mind means several components can be combined in a certain way to produce the same type of result. In Node.js it’s possible to compose powerful pieces of code by piping data to and from other smaller pieces of code, using streams.  </p>\n<h2>Why streams</h2>\n<p>Streams basically provide two major advantages compared to other data handling methods:</p>\n<ol>\n<li><strong>Memory efficiency:</strong> you don’t need to load large amounts of data in memory before you are able to process it</li>\n<li><strong>Time efficiency:</strong> it takes significantly less time to start processing data as soon as you have it, rather than having to wait with processing until the entire payload has been transmitted </li>\n</ol>\n<h2>There are 4 types of streams in Node.js:</h2>\n<ol>\n<li><strong>Writable:</strong> streams to which we can write data. For example, <code>fs.createWriteStream()</code> lets us write data to a file using streams.</li>\n<li><strong>Readable:</strong> streams from which data can be read. For example: <code>fs.createReadStream()</code> lets us read the contents of a file.</li>\n<li><strong>Duplex:</strong> streams that are both Readable and Writable. For example, <code>net.Socket</code></li>\n<li><strong>Transform:</strong>  streams that can modify or transform the data as it is written and read. For example, in the instance of file-compression, you can write compressed data and read decompressed data to and from a file.</li>\n</ol>\n<p>If you have already worked with Node.js, you may have come across streams. For example, in a Node.js based HTTP server, <code>request</code> is a readable stream and <code>response</code> is a writable stream. You might have used the <code>fs</code> module, which lets you work with both readable and writable file streams. Whenever you’re using Express you are using streams to interact with the client, also, streams are being used in every database connection driver that you can work with, because of TCP sockets, TLS stack and other connections are all based on Node.js streams.</p>\n<h1>A practical example</h1>\n<h2>How to create a readable stream</h2>\n<p>We first require the Readable stream, and we initialize it.</p>\n<pre><code class=\"language-javascript\">const Stream = require('stream')\nconst readableStream = new Stream.Readable()\n</code></pre>\n<p>Now that the stream is initialized, we can send data to it:</p>\n<pre><code class=\"language-javascript\">readableStream.push('ping!')\nreadableStream.push('pong!')\n</code></pre>\n<h2>async iterator</h2>\n<p><strong>It’s highly recommended to use async iterator when working with streams.</strong> According to <a href=\"https://twitter.com/rauschma\">Dr. Axel Rauschmayer</a>, Asynchronous iteration is a protocol for retrieving the contents of a data container asynchronously (meaning the current “task” may be paused before retrieving an item). Also, it’s important to mention that the stream async iterator implementation use the ‘readable’ event inside.</p>\n<p>You can use async iterator when reading from readable streams:</p>\n<pre><code>import * as fs from 'fs';\n\nasync function logChunks(readable) {\n  for await (const chunk of readable) {\n    console.log(chunk);\n  }\n}\n\nconst readable = fs.createReadStream(\n  'tmp/test.txt', {encoding: 'utf8'});\nlogChunks(readable);\n\n// Output:\n// 'This is a test!\\n'\n</code></pre>\n<p><strong>It’s also possible to collect the contents of a readable stream in a string:</strong> </p>\n<pre><code>import {Readable} from 'stream';\n\nasync function readableToString2(readable) {\n  let result = '';\n  for await (const chunk of readable) {\n    result += chunk;\n  }\n  return result;\n}\n\nconst readable = Readable.from('Good morning!', {encoding: 'utf8'});\nassert.equal(await readableToString2(readable), 'Good morning!');\n</code></pre>\n<p>Note that, in this case, we had to use an async function because we wanted to return a Promise.</p>\n<p>It’s important to keep in mind to not mix async functions with <code>EventEmitter</code> because  currently, there is no way to catch a rejection when it is emitted within an event handler, causing hard to track bugs and memory leaks. The best current practice is to always wrap the content of an async function in a try/catch block and handle errors, but this is error prone. <a href=\"https://github.com/nodejs/node/pull/27867\">This pull request</a> aims to solve this issue once it lands on Node core.</p>\n<p>To learn more about Node.js streams via async iteration, check out <a href=\"https://2ality.com/2019/11/nodejs-streams-async-iteration.html\">this great article</a>.</p>\n<h3>Readable.from(): Creating readable streams from iterables</h3>\n<p><code>stream.Readable.from(iterable, [options])</code> it’s a utility method for creating Readable Streams out of iterators, which holds the data contained in iterable. Iterable can be a synchronous iterable or an asynchronous iterable. The parameter options is optional and can, among other things, be used to specify a text encoding.</p>\n<pre><code>const { Readable } = require('stream');\n\nasync function * generate() {\n  yield 'hello';\n  yield 'streams';\n}\n\nconst readable = Readable.from(generate());\n\nreadable.on('data', (chunk) => {\n  console.log(chunk);\n});\n</code></pre>\n<h3>Two Reading Modes</h3>\n<p>According to <a href=\"https://nodejs.org/api/stream.html#stream_stream\">Streams API</a>, readable streams effectively operate in one of two modes: <em>flowing</em> and <em>paused</em>. A Readable stream can be in object mode or not, regardless of whether it is in flowing mode or paused mode.</p>\n<ul>\n<li>In <strong>flowing mode</strong>, data is read from the underlying system automatically and provided to an application as quickly as possible using events via the EventEmitter interface.</li>\n<li>In <strong>paused mode</strong>, the <code>stream.read()</code> method must be called explicitly to read chunks of data from the stream.</li>\n</ul>\n<p><strong>In a flowing mode</strong>, to read data from a stream, it’s possible to listen to data event and attach a callback. When a chunk of data is available, the readable stream emits a data event and your callback executes. Take a look at the following snippet:</p>\n<pre><code class=\"language-javascript\">var fs = require(\"fs\");\nvar data = '';\n\nvar readerStream = fs.createReadStream('file.txt'); //Create a readable stream\n\nreaderStream.setEncoding('UTF8'); // Set the encoding to be utf8. \n\n// Handle stream events --> data, end, and error\nreaderStream.on('data', function(chunk) {\n   data += chunk;\n});\n\nreaderStream.on('end',function() {\n   console.log(data);\n});\n\nreaderStream.on('error', function(err) {\n   console.log(err.stack);\n});\n\nconsole.log(\"Program Ended\");\n</code></pre>\n<p>The function call <code>fs.createReadStream()</code> gives you a readable stream. Initially, the stream is in a static state. As soon as you listen to data event and attach a callback it starts flowing. After that, chunks of data are read and passed to your callback. The stream implementor decides how often a data event is emitted. For example, an HTTP request may emit a data event once every few KBs of data are read. When you are reading data from a file you may decide you emit a data event once a line is read.</p>\n<p>When there is no more data to read (end is reached), the stream emits an end event. In the above snippet, we listen to this event to get notified when the end is reached.</p>\n<p>Also, if there is an error, the stream will emit and notify the error.</p>\n<p><strong>In paused mode</strong>, you just need to call read() on the stream instance repeatedly until every chunk of data has been read, like in the following example: </p>\n<pre><code class=\"language-javascript\">var fs = require('fs');\nvar readableStream = fs.createReadStream('file.txt');\nvar data = '';\nvar chunk;\n\nreadableStream.on('readable', function() {\n    while ((chunk=readableStream.read()) != null) {\n        data += chunk;\n    }\n});\n\nreadableStream.on('end', function() {\n    console.log(data)\n});\n</code></pre>\n<p>The read() function reads some data from the internal buffer and returns it. When there is nothing to read, it returns null. So, in the while loop, we check for null and terminate the loop. Note that the readable event is emitted when a chunk of data can be read from the stream.</p>\n<hr>\n<p>All <code>Readable</code> streams begin in <strong>paused mode</strong> but can be switched to <strong>flowing mode</strong> in one of the following ways:</p>\n<ul>\n<li>Adding a 'data' event handler.</li>\n<li>Calling the <code>stream.resume()</code> method.</li>\n<li>Calling the <code>stream.pipe()</code> method to send the data to a Writable.</li>\n</ul>\n<p>The <code>Readable</code> can switch back to paused mode using one of the following:</p>\n<ul>\n<li>If there are no pipe destinations, by calling the <code>stream.pause()</code> method.</li>\n<li>If there are pipe destinations, by removing all pipe destinations. Multiple pipe destinations may be removed by calling the <code>stream.unpipe()</code> method.</li>\n</ul>\n<p>The important concept to remember is that a <code>Readable</code> will not generate data until a mechanism for either consuming or ignoring that data is provided. If the consuming mechanism is disabled or taken away, the <code>Readable</code> will <em>attempt</em> to stop generating the data.\nAdding a <code>readable</code> event handler automatically make the stream to stop flowing, and the data to be consumed via <code>readable.read()</code>. If the 'readable' event handler is removed, then the stream will start flowing again if there is a 'data' event handler.</p>\n<h2>How to create a writable stream</h2>\n<p>To write data to a writable stream you need to call <code>write()</code> on the stream instance. Like in the following example:</p>\n<pre><code class=\"language-javascript\">var fs = require('fs');\nvar readableStream = fs.createReadStream('file1.txt');\nvar writableStream = fs.createWriteStream('file2.txt');\n\nreadableStream.setEncoding('utf8');\n\nreadableStream.on('data', function(chunk) {\n    writableStream.write(chunk);\n});\n</code></pre>\n<p>The above code is straightforward. It simply reads chunks of data from an input stream and writes to the destination using <code>write()</code>. This function returns a boolean value indicating if the operation was successful. If true, then the write was successful and you can keep writing more data. If false is returned, it means something went wrong and you can’t write anything at the moment. The writable stream will let you know when you can start writing more data by emitting a drain event.</p>\n<p>Calling the <code>writable.end()</code> method signals that no more data will be written to the Writable. If provided, the optional callback function is attached as a listener for the 'finish' event.</p>\n<pre><code class=\"language-javascript\">// Write 'hello, ' and then end with 'world!'.\nconst fs = require('fs');\nconst file = fs.createWriteStream('example.txt');\nfile.write('hello, ');\nfile.end('world!');\n// Writing more now is not allowed!\n</code></pre>\n<p><strong>Using a writable stream you can read data from a readable stream:</strong></p>\n<pre><code class=\"language-javascript\">const Stream = require('stream')\n\nconst readableStream = new Stream.Readable()\nconst writableStream = new Stream.Writable()\n\nwritableStream._write = (chunk, encoding, next) => {\n    console.log(chunk.toString())\n    next()\n}\n\nreadableStream.pipe(writableStream)\n\nreadableStream.push('ping!')\nreadableStream.push('pong!')\n\nwritableStream.end()\n</code></pre>\n<p><strong>You can also use async iterators to write to a writable stream, which is recommended</strong></p>\n<pre><code>import * as util from 'util';\nimport * as stream from 'stream';\nimport * as fs from 'fs';\nimport {once} from 'events';\n\nconst finished = util.promisify(stream.finished); // (A)\n\nasync function writeIterableToFile(iterable, filePath) {\n  const writable = fs.createWriteStream(filePath, {encoding: 'utf8'});\n  for await (const chunk of iterable) {\n    if (!writable.write(chunk)) { // (B)\n      // Handle backpressure\n      await once(writable, 'drain');\n    }\n  }\n  writable.end(); // (C)\n  // Wait until done. Throws if there are errors.\n  await finished(writable);\n}\n\nawait writeIterableToFile(\n  ['One', ' line of text.\\n'], 'tmp/log.txt');\nassert.equal(\n  fs.readFileSync('tmp/log.txt', {encoding: 'utf8'}),\n  'One line of text.\\n');\n</code></pre>\n<p>The default version of stream.finished() is callback-based but can be turned into a Promise-based version via util.promisify() (line A).</p>\n<p>In this example, it is used the following two patterns:</p>\n<p>Writing to a writable stream while handling backpressure (line B):</p>\n<pre><code>if (!writable.write(chunk)) {\n  await once(writable, 'drain');\n}\n</code></pre>\n<p>Closing a writable stream and waiting until writing is done (line C):</p>\n<pre><code>writable.end();\nawait finished(writable);\n</code></pre>\n<h2>pipeline()</h2>\n<p>Piping is a mechanism where we provide the output of one stream as the input to another stream. It is normally used to get data from one stream and to pass the output of that stream to another stream. There is no limit on piping operations. In other words, piping is used to process streamed data in multiple steps.</p>\n<p>In Node 10.x was introduced <code>stream.pipeline()</code>. This is a module method to pipe between streams forwarding errors and properly cleaning up and provide a callback when the pipeline is complete.</p>\n<p>Here is an example of using pipeline:</p>\n<pre><code class=\"language-javascript\">const { pipeline } = require('stream');\nconst fs = require('fs');\nconst zlib = require('zlib');\n\n// Use the pipeline API to easily pipe a series of streams\n// together and get notified when the pipeline is fully done.\n// A pipeline to gzip a potentially huge video file efficiently:\n\npipeline(\n  fs.createReadStream('The.Matrix.1080p.mkv'),\n  zlib.createGzip(),\n  fs.createWriteStream('The.Matrix.1080p.mkv.gz'),\n  (err) => {\n    if (err) {\n      console.error('Pipeline failed', err);\n    } else {\n      console.log('Pipeline succeeded');\n    }\n  }\n);\n</code></pre>\n<p><code>pipeline</code> should be used instead of <code>pipe</code>, as pipe is unsafe.</p>\n<h2>The Stream Module</h2>\n<p>The <a href=\"https://nodejs.org/api/stream.html\">Node.js stream module</a> provides the foundation upon which all streaming APIs are build.</p>\n<p>The Stream module is a native module that shipped by default in Node.js. The Stream is an instance of the EventEmitter class which handles events asynchronously in Node. Because of this, streams are inherently event-based.</p>\n<p>To access the stream module:</p>\n<pre><code class=\"language-javascript\">const stream = require('stream');\n</code></pre>\n<p>The <code>stream</code> module is useful for creating new types of stream instances. It is usually not necessary to use the <code>stream</code> module to consume streams.</p>\n<h2>Streams-powered Node APIs</h2>\n<p>Due to their advantages, many Node.js core modules provide native stream handling capabilities, most notably:</p>\n<ul>\n<li><code>net.Socket</code> is the main node api that is stream are based on, which underlies most of the following APIs</li>\n<li><code>process.stdin</code> returns a stream connected to stdin</li>\n<li><code>process.stdout</code> returns a stream connected to stdout</li>\n<li><code>process.stderr</code> returns a stream connected to stderr</li>\n<li><code>fs.createReadStream()</code> creates a readable stream to a file</li>\n<li><code>fs.createWriteStream()</code> creates a writable stream to a file</li>\n<li><code>net.connect()</code> initiates a stream-based connection</li>\n<li><code>http.request()</code> returns an instance of the http.ClientRequest class, which is a writable stream</li>\n<li><code>zlib.createGzip()</code> compress data using gzip (a compression algorithm) into a stream</li>\n<li><code>zlib.createGunzip()</code> decompress a gzip stream.</li>\n<li><code>zlib.createDeflate()</code> compress data using deflate (a compression algorithm) into a stream</li>\n<li><code>zlib.createInflate()</code> decompress a deflate stream</li>\n</ul>\n<h2>Streams Cheat Sheet:</h2>\n<p><img src=\"//images.ctfassets.net/hspc7zpa5cvq/3s8p9qQe6zDEfhwLyq5fQR/7ce28dd384a590009aaaa16886bb83eb/cheat1.png\" alt=\"cheat1\"> <img src=\"//images.ctfassets.net/hspc7zpa5cvq/1vZrS7rb7U7DRb5TtT8hs8/bb6285794d4141c3a572770651fcc753/cheat2.png\" alt=\"cheat2\">\n<img src=\"//images.ctfassets.net/hspc7zpa5cvq/2rPAh1mmsLg02i0hLBG2DG/9829c1fb9cf396d952f1d155ce799f13/cheat3.png\" alt=\"cheat3\">\n<img src=\"//images.ctfassets.net/hspc7zpa5cvq/3HncUkiyjekdWjN8MwQbfi/29b3ad1806e8ce161a889c76667e780f/cheat3.png\" alt=\"cheat3\"> </p>\n<center>![Webp.net-resizeimage](//images.ctfassets.net/hspc7zpa5cvq/24pAlF9SE7EYkg4Xb2CtlW/8ae386aaf08e513372a09100bb8cd48a/Webp.net-resizeimage.png)</center>\n<center>See more: [Node.js streams cheatsheet](https://devhints.io/nodejs-stream)</center>\n<p>Here are some important events related to writable streams:</p>\n<ul>\n<li><code>error</code> – Emitted to indicate that an error has occurred while writing/piping.</li>\n<li><code>pipeline</code> – When a readable stream is piped into a writable stream, this event is emitted by the writable stream.</li>\n<li><code>unpipe</code> – Emitted when you call unpipe on the readable stream and stop it from piping into the destination stream.</li>\n</ul>\n<h2>Conclusion</h2>\n<p>This was all about the basics of streams. Streams, pipes, and chaining are the core and most powerful features in Node.js. Streams can indeed help you write neat and performant code to perform I/O.</p>\n<p>Also, there is a <a href=\"https://github.com/nodejs/TSC/blob/master/Strategic-Initiatives.md#current-initiatives\">Node.js strategic initiative</a> worth looking to, called <a href=\"https://github.com/Fishrock123/bob\">BOB</a>, aiming to improve Node.js streaming data interfaces, both within Node.js core internally, and hopefully also as future public APIs.</p>\n<h4>References</h4>\n<p>Special thanks to <a href=\"https://twitter.com/matteocollina\">Matteo Colina</a> and <a href=\"https://twitter.com/Fishrock123\">Jeremiah Senkpiel</a> for your feedback!</p>\n<ul>\n<li><a href=\"https://nodejs.org/api/stream.html\">Stream API</a></li>\n<li><a href=\"https://www.freecodecamp.org/news/node-js-streams-everything-you-need-to-know-c9141306be93/\">Node.js Streams: Everything you need to know</a></li>\n<li><a href=\"https://flaviocopes.com/nodejs-streams/\">Node.js Streams</a></li>\n<li><a href=\"https://www.sitepoint.com/basics-node-js-streams/\">The Basics of Node.js Streams</a></li>\n<li><a href=\"https://devhints.io/nodejs-stream\">Node.js streams cheatsheet</a></li>\n<li><a href=\"https://www.tutorialspoint.com/nodejs/nodejs_streams.htm\">Node.js - Streams</a></li>\n<li><a href=\"https://2ality.com/2019/11/nodejs-streams-async-iteration.html\">Easier Node.js streams via async iteration</a></li>\n<li><a href=\"https://changelog.com/jsparty/103\">You’re probably using streams</a></li>\n</ul>"}},"categories":[{"name":"Node.js","slug":"node-js"},{"name":"Tutorials","slug":"tutorials"}]}},{"node":{"slug":"memory-leaks-demystified","title":"Memory Leaks Demystified","image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/5CevI6czzcOyc5GXJBAmxQ/fe0582222c66618590ef6b7752f8913a/20-min.jpg","fileName":"20-min.jpg"}},"bodyContent":{"childMarkdownRemark":{"html":"<p>Tracking down memory leaks in Node.js has been a recurring topic, people are always interested in learning more about due to the complexity and the range of causes. </p>\n<p>Not all memory leaks are immediately obvious - quite the opposite; however once we identify a pattern, we must look for a correlation between memory usage, objects held in memory and response time. When examining objects, look into how many of them are collected, and whether any of them are usual, depending on the framework or technique used to serve the content (ex. Server Side Rendering). Hopefully, after you finish this article, you'll be able to understand, and look for a strategy to debug the memory consumption of a Node.js application.</p>\n<h2>Garbage Collection Theory in Node.js</h2>\n<p>JavaScript is a garbage collected language and Google’s V8 is a JavaScript engine, initially created for Google Chrome, that can be used as a standalone runtime in many instances. Two important operations of the Garbage Collector in Node.js are: </p>\n<ol>\n<li>identify live or dead objects and </li>\n<li>recycle/reuse the memory occupied by dead objects. </li>\n</ol>\n<p>Something important to keep in mind: When the Garbage Collector runs, it pauses your application entirely until it finishes its work. As such you will need to minimize its work by taking care of your objects’ references.</p>\n<p>All memory used by a Node.js process is being automatically allocated and de-allocated by the V8 JavaScript engine. Let’s see how this looks in practice.</p>\n<p>If you think of memory as a graph, then imagine V8 keeping a graph of all variables in the program, starting from the ‘Root node’. This could be your window or the global object in a Node.js module, usually known as the dominator. Something important to keep in mind is you don’t control how this Root node is de-allocated.</p>\n<center>![1](//images.ctfassets.net/hspc7zpa5cvq/5Q1GH4J2wTOELQgOuadwt1/f9fa0ffa99002c25b4300da2b8a6291a/1.png)</center>\n<p>Next, you’ll find an Object node, usually known as leaves (there are no child references). Finally, there are 4 types of data types in JavaScript: Boolean, String, Number, and Object.</p>\n<p>V8 will walk through the graph and try to identify groups of data that can no longer be reached from the Root node. If it’s not reachable from the Root node, V8 assumes that the data is no longer used and releases the memory. Remember: to determine whether an object is live, it is necessary to check if is reachable through some chain of pointers from an object which is live by definition; everything else, such as an object being unreachable from a root node or not referenceable by a root node or another live object is considered garbage.</p>\n<p>In a nutshell, the garbage collector has two main tasks; </p>\n<ol>\n<li>trace and  </li>\n<li>count references between objects. </li>\n</ol>\n<p>It can get tricky when you need to track remote references from another process, but in Node.js applications, we use a single process which makes our life a bit easier.</p>\n<h2>V8’s Memory Scheme</h2>\n<p>V8 uses a scheme similar to the Java Virtual Machine and divides the memory into segments. The thing that wraps the scheme concept is known as Resident Set, which refers to the portion of memory occupied by a process that is held in the RAM. </p>\n<p>Inside the Resident Set you will find:</p>\n<ul>\n<li><strong>Code Segment:</strong> Where the actual code is being executed.</li>\n<li><strong>Stack:</strong> Contains local variables and all value types with pointers referencing objects on the heap or defining the control flow of the application.</li>\n<li><strong>Heap:</strong> A memory segment dedicated to storing reference types like objects, strings and closures.</li>\n</ul>\n<center>![2](//images.ctfassets.net/hspc7zpa5cvq/70JYMG5lfWa2ttKVi9p9jw/6189bbfd894a95e3e9fa7424e4ac5e68/2.png)</center>\n<p>Two more important things to keep in mind:</p>\n<ul>\n<li><strong>Shallow size of an object:</strong> the size of memory that is held by the object itself</li>\n<li><strong>Retained size of an object:</strong> the size of the memory that is freed up once the object is deleted along with its' dependent objects</li>\n</ul>\n<p>Node.js has an object describing the memory usage of the Node.js process measured in bytes. Inside the object you’ll find:</p>\n<ul>\n<li><strong>rss:</strong> Refers to resident set size.</li>\n<li><strong>heapTotal and heapUsed:</strong> Refers to V8's memory usage.</li>\n<li><strong>external:</strong> refers to the memory usage of C++ objects bound to JavaScript objects managed by V8.</li>\n</ul>\n<h2>Finding the leak</h2>\n<p>Chrome DevTools is a great tool that can be used to diagnose memory leaks in Node.js applications via remote debugging. Other tools exist and they will give you the similar. This blog post relies on one of those different tools  in order to give you a clear clear understanding of what is happening. However, you need to keep in mind that profiling is an intensive CPU task, which can impact your application negatively. Be aware!</p>\n<p>The Node.js application we are going to profile is a simple HTTP API Server that has multiple endpoints, returning different information to whoever is consuming the service. You can clone the <a href=\"https://github.com/nodesource/memory-leak-example\">repository</a> of the Node.js application used here.</p>\n<pre><code class=\"language-javascript\">const http = require('http')\n\nconst leak = []\n\nfunction requestListener(req, res) {\n\n  if (req.url === '/now') {\n    let resp = JSON.stringify({ now: new Date() })\n    leak.push(JSON.parse(resp))\n    res.writeHead(200, { 'Content-Type': 'application/json' })\n    res.write(resp)\n    res.end()\n  } else if (req.url === '/getSushi') {\n    function importantMath() {\n    let endTime = Date.now() + (5 * 1000);\n    while (Date.now() &#x3C; endTime) {\n        Math.random();\n    }\n    }\n\n    function theSushiTable() {\n    return new Promise(resolve => {\n        resolve('🍣');\n    });\n    }\n\n    async function getSushi() {\n    let sushi = await theSushiTable();\n    res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })\n    res.write(`Enjoy! ${sushi}`);\n    res.end()\n    }\n\n    getSushi()\n    importantMath()\n  } else {\n    res.end('Invalid request')\n  }\n}\n\nconst server = http.createServer(requestListener)\nserver.listen(process.env.PORT || 3000)\n</code></pre>\n<p>Start the Node.js application:</p>\n<center>![3](//images.ctfassets.net/hspc7zpa5cvq/7qPrtjQABesHin79yHkpx8/3f58792f45fc83b3b29be47bb18276ba/3.png)</center>\n<p>We have been using a 3S (3 Snapshot) approach to diagnostics and identify possible memory issues. Interesting enough, we found this was an approach that has been used by Loreena Lee at the Gmail team for a long time to solve memory issues. A walkthrough for this approach:</p>\n<ol>\n<li>Open Chrome DevTools and visit <code>chrome://inspect</code>.</li>\n<li>Click on the <code>inspect</code> button from one of your applications in the Remote Target section located at the bottom.</li>\n</ol>\n<center>![4](//images.ctfassets.net/hspc7zpa5cvq/00fl4PWM6ypQsAQvQax4z/4b213a23b59d041443c79baff286d462/4.png)</center>\n<p><strong>Note:</strong> Make sure you have the Inspector attached to the Node.js application you want to profile. You can also connect to Chrome DevTools using <code>ndb</code> .</p>\n<p>You are going to see a <code>Debugger Connected</code> message in the output of your console when the app is running.</p>\n<ol start=\"3\">\n<li>Go to Chrome DevTools > Memory</li>\n<li>Take a heap snapshot</li>\n</ol>\n<center>![5](//images.ctfassets.net/hspc7zpa5cvq/508s4XafhJFc9SEFTro4Oy/8b77c46e3f90f5e2e833263b139b03ba/5.png)</center>\n<p>In this case, we took the first snapshot without any load or processing being done by the service. This a tip for certain use-cases: it’s fine if we are completely sure the application doesn’t require any warm up before accepting request or do some processing. Sometimes it makes sense to do a warm-up action before taking the first heap snapshot as there are cases where you might be doing lazy initialization for global variables on the first invocation.</p>\n<ol start=\"5\">\n<li>Perform the action in your app that you think is causing leaks in memory.</li>\n</ol>\n<p>In this case we are going to run <code>npm run load-mem</code>. This will start <code>ab</code> to simulate traffic/load in your Node.js application.</p>\n<center>![6](//images.ctfassets.net/hspc7zpa5cvq/3C8odWznGsQLHrI6Rk7AiR/cee2db5cb10445625ccef140094e7ac2/6.png)</center>\n<ol start=\"6\">\n<li>Take a heap snapshot</li>\n</ol>\n<center>![7](//images.ctfassets.net/hspc7zpa5cvq/7qhN45E63hj6VNgCmzgbJK/310166a0f21d6a209fb188856d1d114e/7.png)</center>\n<ol start=\"7\">\n<li>Again, perform the action in your app that you think is causing leaks in memory.</li>\n<li>Take a final heap snapshot</li>\n</ol>\n<center>![8](//images.ctfassets.net/hspc7zpa5cvq/64kDYnKCQZDDyvPmlJPEnd/eba0e39c9234ff4f504de36426e5f559/8.png)</center>\n<ol start=\"9\">\n<li>Select the latest snapshot taken.</li>\n<li>At the top of the window, find the drop-down that says “All objects” and switch this to “Objects allocated between snapshots 1 and 2”. (You can also do the same for 2 and 3 if needed). This will substantially cut down on the number of objects that you see.</li>\n</ol>\n<center>![9](//images.ctfassets.net/hspc7zpa5cvq/4urwJ9qTrKeSmVoZr2nuBK/f789d6a4bfc37f9dbc10544cdde30039/9.png)</center>\n<p>The Comparison view can help you identify those Objects too:</p>\n<center>![10](//images.ctfassets.net/hspc7zpa5cvq/gLdojUXBm73CtqWK6RLKQ/d0b8452f255e29a76cd1c768e676478b/10.png)</center>\n<p>In the view you’ll see a list of leaked objects that are still hanging around, top level entries (a row per constructor), columns for distance of the object to the GC root, number of object instances, shallow size and retained size. You can select one to see what is being retained in its retaining tree. A good rule of thumb is to first ignore the items wrapped in parenthesis as those are built-in structures. The <code>@</code> character is objects’ unique ID, allowing you to compare heap snapshots on per-object basis.</p>\n<p>A typical memory leak might retain a reference to an object that’s expected to only last during one request cycle by accidentally storing a reference to it in a global object that cannot be garbage collected.</p>\n<p>This example generates a random object with the date timestamp when the request was made to imitate an application object that might be returned from an API query and purposefully leak it by storing it in a global array. Looking at a couple of the retained Object’s you can see some examples of the data that has been leaked, which you can use to track down the leak in your application.</p>\n<p>NSolid is great for this type of use-case, because it gives you a really good insight of how memory is increasing on every task or load-test you perform. You can also see in real time how every profiling action impacts CPU if you were curious.</p>\n<center>![demo](//downloads.ctfassets.net/hspc7zpa5cvq/15FePdRbw0mgW7bUb9RFAI/01c81ff74635922a9352d29fa64446cd/demo.gif)</center>\n<p>In real world situations, memory leaks happen when you are not looking at the tool you use to monitor your application, something great about NSolid is the ability to set thresholds and limits for different metrics of your application. For example, you can set NSolid to take a heap snapshot if more than X amount of memory is being used or during X time memory hasn’t recovered from a high consumption spike. Sounds great, right?</p>\n<div class=\"blog-cta nsolid\">\n    Get started with low-impact memory leaks monitoring\n  <a class=\"button more large\" href=\"https://accounts.nodesource.com/sign-up-lambda-blogref-m\">Create your NodeSource Account</a>\n</div>\n<h2>Marking and Sweeping</h2>\n<p><strong>V8</strong>’s garbage collector is mainly based on the <strong>Mark-Sweep</strong> collection algorithm which consists of tracing garbage collection that operates by marking reachable objects, then sweeping over memory and recycling objects that are unmarked (which must be unreachable), putting them on a free list. This is also known as a generational garbage collector where objects may move within the young generation, from the young to the old generation, and within the old generation. </p>\n<p>Moving objects is expensive since the underlying memory of objects needs to be copied to new locations and the pointers to those objects are also subject to updating.</p>\n<p>For mere mortals, this could be translated to:</p>\n<p>V8 looks recursively for all objects’ reference paths to the Root node. For example: In JavaScript, the \"window\" object is an example of a global variable that can act as a Root. The window object is always present, so the garbage collector can consider it and all of its children to be always present (i.e. not garbage). If any reference has no path to the Root node. especially when it looks for unreferenced objects recursively, it will be marked as garbage and will be swept later to free that memory and return it to the OS.</p>\n<p>However, modern garbage collectors improve on this algorithm in different ways, but the essence is the same: reachable pieces of memory are marked as such and the rest is considered garbage.</p>\n<p>Remember, everything that can be reached from a Root is not considered garbage. Unwanted references are variables kept somewhere in the code that will not be used anymore and point to a piece of memory that could otherwise be freed, so to understand the most common leaks in JavaScript, we need to know the ways references are commonly forgotten.</p>\n<h2>The Orinoco Garbage Collector</h2>\n<p>Orinoco is the codename of the latest GC project to make use of the latest and greatest parallel, incremental and concurrent technique for garbage collection, featuring the ability to free the main thread. One of the significant metrics describing Orinoco’s performance is how often and how long the main thread pauses while the garbage collector performs its functions. For classic ‘stop-the-world’ collectors, these time-intervals impact the application’s user experience due to delays, poor-quality rendering, and an increase in response time. </p>\n<p>V8 distributes the work of garbage collection between auxiliary streams in young memory (scavenging). Each stream receives a set of pointers,  followed by moving all living objects into <em>“to-space”</em>.</p>\n<p>When moving objects into ‘to-space’, threads need to synchronize through atomic read / write / compare and swap operations to avoid a situation where, for example, another thread found the same object, but followed a different path, and tries to move it. </p>\n<p>Quote <a href=\"https://v8.dev/blog/trash-talk\">from V8 page</a>:</p>\n<blockquote>\n<p>Adding parallel, incremental and concurrent techniques to the existing GC was a multi-year effort, but has paid off, moving a lot of work to background tasks. It has drastically improved pause times, latency, and page load, making animation, scrolling, and user interaction much smoother. The parallel Scavenger has reduced the main thread young generation garbage collection total time by about 20%–50%, depending on the workload. Idle-time GC can reduce Gmail’s JavaScript heap memory by 45% when it is idle. Concurrent marking and sweeping has reduced pause times in heavy WebGL games by up to 50%.</p>\n</blockquote>\n<p>The Mark-Evacuate collector consists of three phases: marking, copying, and updating pointers. To avoid sweeping pages in the young generation to maintain free lists, the young generation is still maintained using a semi-space that is always kept compact by copying live objects into “to-space” during garbage collection. It's advantage being parallel is that <strong>‘exact liveness’</strong> information is available. This information can be used to avoid copying by just moving and relinking pages that contain mostly live objects, which is also performed by the full Mark-Sweep-Compact collector. It works by marking  live objects in the heap in the same fashion as the mark-sweep algorithm, meaning the heap will often be fragmented. V8 currently ships with the parallel Scavenger which <strong>reduces the main thread young generation garbage collection total time by about 20%–50%</strong> across a large set of benchmarks.</p>\n<p>Everything related to  pausing of the main thread, response time and page load has significantly improved, which allows animations, scrolling and user interaction on the page to be much smoother. The parallel collector made it possible to reduce the total duration of processing of young memory by 20–50%, depending on the load. However, the work is not over: Reducing pauses remains an important task to simplify the lives of web users, and we continue to look for the possibility of using more advanced techniques to achieve the goal.</p>\n<h2>Conclusions</h2>\n<p>Most developers don’t need to think about GC when developing JavaScript programs, but understanding some of the internals can help you think about memory usage and helpful programming patterns. For example, given the structure of the heap in V8, based on generations, low-living objects are actually quite cheap in terms of GC, since we pay mainly for the surviving objects. This kind of pattern is not only particular to JavaScript but also to many languages with garbage collection support.</p>\n<h3>Main Takeaways:</h3>\n<ul>\n<li>Do not use outdated or deprecated packages like node-memwatch, node-inspector or v8-profiler to inspect and learn about memory. Everything you need is already integrated in the Node.js binary (especially a node.js inspector and debugger). If you need more specialized tooling, you can use <a href=\"https://nodesource.com/products/nsolid\">NSolid</a>, Chrome DevTools and other well known software.</li>\n<li>Consider where and when you trigger heap snapshots and CPU profiles. You will want to trigger both, mostly in testing, due to the intensity of CPU operations that are required to take a snapshot in production. Also, be sure of how many heap-dumps are fine to write out before shutting the process and causing a cold restart.</li>\n<li>There’s no one tool for everything. Test, measure, decide and resolve depending on the application. Choose the best tool for your architecture and the one that delivers more useful data to figure out the issue.</li>\n</ul>\n<h4>References</h4>\n<ul>\n<li><a href=\"https://www.memorymanagement.org\">Memory Management Reference</a></li>\n<li><a href=\"https://v8.dev/blog/trash-talk\">Trash talk: the Orinoco garbage collector</a>\n<a href=\"https://github.com/thlorenz/v8-perf\">v8-perf</a></li>\n<li><a href=\"https://addyosmani.com/blog/taming-the-unicorn-easing-javascript-memory-profiling-in-devtools/\">Taming The Unicorn: Easing JavaScript Memory Profiling In Chrome DevTools</a></li>\n<li><a href=\"https://developer.chrome.com/devtools/docs/javascript-memory-profiling\">JavaScript Memory Profiling</a></li>\n<li><a href=\"https://developer.chrome.com/devtools/docs/memory-analysis-101\">Memory Analysis 101</a></li>\n<li><a href=\"https://speakerdeck.com/addyosmani/javascript-memory-management-masterclass\">Memory Management Masterclass</a></li>\n<li><a href=\"https://www.youtube.com/watch?v=L3ugr9BJqIs\">The Breakpoint Ep. 8: Memory Profiling with Chrome DevTools</a></li>\n<li><a href=\"https://www.youtube.com/watch?v=taADm6ndvVo\">Thorsten Lorenz - Memory Profiling for Mere Mortals</a></li>\n<li><a href=\"https://docs.google.com/presentation/d/1wUVmf78gG-ra5aOxvTfYdiLkdGaR9OhXRnOlIcEmu2s/pub?start=false&#x26;loop=false&#x26;delayms=3000#slide=id.g1d65bdf6_0_0\">Eliminating Memory Leaks in Gmail</a></li>\n</ul>"}},"categories":[{"name":"Node.js","slug":"node-js"},{"name":"Tutorials","slug":"tutorials"}]}},{"node":{"slug":"the-basics-of-package-json-in-node-js-and-npm","title":"The Basics of Package.json in Node.js and npm","image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/2Fdb4tkf4JgHLCFEzpSWJg/7cc53c0c539a56b2fea629b3daa5b8cc/19-min.jpg","fileName":"19-min.jpg"}},"bodyContent":{"childMarkdownRemark":{"html":"<p>This article was first published in <a href=\"https://nodesource.com/blog/the-basics-of-package-json-in-node-js-and-npm/\">NodeSource blog</a> on March 2017.</p>\n<hr>\n<p>The <code>package.json</code> file is core to the Node.js ecosystem and is a basic part of understanding and working with Node.js, npm, and even modern JavaScript. The <code>package.json</code> is used as what equates to a manifest about applications, modules, packages, and more - it's a tool to that's used to make modern development streamlined, modular, and efficient.</p>\n<p>As a developer in the Node.js ecosystem, understanding the basics of <code>package.json</code> is one of the first steps to really kicking off your development experience with Node.js.</p>\n<p>Because of how <strong>essential</strong> understanding the basics of <code>package.json</code> is to development with Node.js, I've gone through and outlined some of the most common and important properties of a <code>package.json</code> file that you'll need to use <code>package.json</code> effectively.</p>\n<h2>Identifying Metadata Inside <code>package.json</code></h2>\n<h3>The <code>name</code> property</h3>\n<p>The <code>name</code> property in a <code>package.json</code> file is one of the fundamental components of the <code>package.json</code> structure. At its core, <code>name</code> is a string that is <em>exactly</em> what you would expect - the name of the module that the <code>package.json</code> is describing.</p>\n<p>Inside your <code>package.json</code>, the <code>name</code> property as a string would look something like this:</p>\n<pre><code class=\"language-json\">    \"name\": \"metaverse\"\n</code></pre>\n<p>Despite having only a few material restrictions (a max length of 214 characters, can't begin with <code>.</code> or <code>_</code>, no uppercase letters, and no characters that aren't URL-friendly), one interesting aspect of the <code>name</code> property is that, there have been software ecosystems that have developed standard naming conventions that enable discoverability simply by using the <code>name</code> property. </p>\n<p>A few examples of this kind of namespacing are <a href=\"http://npmsearch.com/?q=babel-plugin\"><code>babel-plugin-</code> for Babel</a> and the the <a href=\"http://npmsearch.com/?q=-loader\">webpack <code>-loader</code></a> tooling.</p>\n<h3>The <code>version</code> property</h3>\n<p>The <code>version</code> property is a key part of a <code>package.json</code>, as it denotes the current version of the module that the <code>package.json</code> file is describing.</p>\n<p>While the <code>version</code> property isn't <em>required</em> to follow <a href=\"https://nodesource.com/blog/semver-a-primer/\">semver</a>, semver is the standard used by the vast majority of modules and projects in the Node.js ecosystem - and the module version, according to semver, is what you'll typically find in the <code>version</code> property of a <code>package.json</code> file.</p>\n<p>Inside your <code>package.json</code>, the <code>version</code> property as a string using semver could look like this:</p>\n<pre><code class=\"language-json\">    \"version\": \"5.12.4\"\n</code></pre>\n<h3>The <code>license</code> property</h3>\n<p>The <code>license</code> property of a <code>package.json</code> file is used to note what license the module that the <code>package.json</code> file is describing. While there are some complex ways you can use the <code>license</code> property of a <code>package.json</code> file (to do things like dual-licensing or defining your own license), the most typical usage of it is to use a <a href=\"https://spdx.org/licenses/\">SPDX License</a> identifier - some examples that you may recognize are <code>MIT</code>, <code>ISC</code>, and <code>GPL-3.0</code>.</p>\n<p>Inside your <code>package.json</code>, the <code>license</code> property with an <code>MIT</code> license look like this:</p>\n<pre><code class=\"language-json\">    \"license\": \"MIT\"\n</code></pre>\n<div class=\"blog-cta nsolid\" style=\"background-color: #4cb5ff;\">\n\tLooking for more info on npm? Check out our complete guide: \n  <a class=\"button more large\" href=\"https://pages.nodesource.com/npm-guide-ultimate-wb.html?utm_campaign=blogref&utm_source=blog&utm_content=blog-npmbasics\">Read now: The Ultimate Guide to npm</a>\n</div>\n<h3>The <code>description</code> property</h3>\n<p>The <code>description</code> property of a <code>package.json</code> file is a string that contains a human-readable description about the module - basically, it's the module developer's chance to quickly let users know what <em>exactly</em> a module does. The <code>description</code> property is frequently indexed by search tools like <a href=\"http://npmsearch.com/\">npm search</a> and the npm CLI search tool to help find relevant packages based on a search query.</p>\n<p>Inside your <code>package.json</code>, the <code>description</code> property would look like this:</p>\n<pre><code class=\"language-json\">    \"description\": \"The Metaverse virtual reality. The final outcome of all virtual worlds, augmented reality, and the Internet.\"\n</code></pre>\n<h3>The <code>keywords</code> property</h3>\n<p>The <code>keywords</code> property inside a <code>package.json</code> file is, as you may have guessed, a collection of keywords about a module. Keywords can help identify a package, related modules and software, and concepts.</p>\n<p>The <code>keywords</code> property is always going to be an array, with one or more strings as the array's values - each one of these strings will, in turn, be one of the project's keywords.</p>\n<p>Inside your <code>package.json</code>, the <code>keywords</code> array would look something like this:</p>\n<pre><code class=\"language-json\">    \"keywords\": [\n        \"metaverse\",\n        \"virtual reality\",\n        \"augmented reality\",\n        \"snow crash\"\n    ]\n</code></pre>\n<h2>Functional Metadata Inside <code>package.json</code></h2>\n<h3>The <code>main</code> property</h3>\n<p>The <code>main</code> property of a <code>package.json</code> is a direction to the entry point to the module that the <code>package.json</code> is describing. In a Node.js application, when the module is called via a require statement, the module's exports from the file named in the <code>main</code> property will be what's returned to the Node.js application.</p>\n<p>Inside your <code>package.json</code>, the <code>main</code> property, with an entry point of <code>app.js</code>, would look like this:</p>\n<pre><code class=\"language-json\">    \"main\": \"app.js\",\n</code></pre>\n<h3>The <code>repository</code> property</h3>\n<p>The <code>repository</code> property of a <code>package.json</code> is an array that defines <em>where</em> the source code for the module lives. Typically, for open source projects, this would be a public GitHub repo, with the <code>repository</code> array noting that the type of version control is <code>git</code>, and the URL of the repo itself. One thing to note about this is that it's not just a URL the repo can be accessed from, but the full URL that the <em>version control</em> can be accessed from.</p>\n<p>Inside your package.json, the <code>repository</code> property would look like this:</p>\n<pre><code class=\"language-json\">  \"repository\": {\n      \"type\": \"git\",\n      \"url\": \"https://github.com/bnb/metaverse.git\"\n  }\n</code></pre>\n<h3>The <code>scripts</code> property</h3>\n<p>The <code>scripts</code> property of a <code>package.json</code> file is simple conceptually, but is complex functionally to the point that it's used as a build tool by many. </p>\n<p>At its simplest, the <code>scripts</code> property takes an object with as many key/value pairs as desired. Each one of the keys in these key/value pairs is the name of a command that can be run. The corresponding value of each key is the actual command that <em>is</em> run. Scripts are frequently used for testing, building, and streamlining of the needed commands to work with a module. </p>\n<p>Inside your package.json, the <code>scripts</code> property with a <code>build</code> command to execute <code>node app.js</code> (presumably to build your application) and a <code>test</code> command using <a href=\"https://github.com/feross/standard\">Standard</a> would look like this:</p>\n<pre><code class=\"language-json\">    \"scripts\": {\n        \"build\": \"node app.js\",\n        \"test\": \"standard\"\n    }\n</code></pre>\n<h3>The <code>dependencies</code> property</h3>\n<p>The <code>dependencies</code> property of a module's <code>package.json</code> is where dependencies - the <em>other</em> modules that <em>this</em> module uses - are defined. The <code>dependencies</code> property takes an object that has the name and version at which each dependency should be used. Tying things back to the <code>version</code> property defined earlier, the version that a module needs is defined. Do note that you'll frequently find carets (<code>^</code>) and tildes (<code>~</code>) included with package versions. These are the notation for version range - taking a deep-dive into these is outside the scope of this article, but you can learn more in our <a href=\"https://nodesource.com/blog/semver-a-primer/\">primer on semver</a>.</p>\n<p>Inside your package.json, the <code>dependencies</code> property of your module may look something like this:</p>\n<pre><code class=\"language-json\">  \"dependencies\": {\n    \"async\": \"^0.2.10\",\n    \"npm2es\": \"~0.4.2\",\n    \"optimist\": \"~0.6.0\",\n    \"request\": \"~2.30.0\",\n    \"skateboard\": \"^1.5.1\",\n    \"split\": \"^0.3.0\",\n    \"weld\": \"^0.2.2\"\n  },\n</code></pre>\n<h3>The <code>devDependencies</code> property</h3>\n<p>The <code>devDependencies</code> property of a <code>package.json</code> is almost identical to the <code>dependencies</code> property in terms of structure, with a key difference. The <code>dependencies</code> property is used to define the dependencies that a module needs to run in <em>production</em>. The <code>devDependencies</code> property is <em>usually</em> used to define the dependencies the module needs to run in <em>development</em>.</p>\n<p>Inside your package.json, the <code>devDependencies</code> property would look something like this:</p>\n<pre><code class=\"language-json\">    \"devDependencies\": {\n        \"escape-html\": \"^1.0.3\",\n        \"lucene-query-parser\": \"^1.0.1\"\n    }\n</code></pre>\n<h2>Want to keep going?</h2>\n<p>If you want to keep learning about Node.js, npm, package.json, and development with the Node.js stack, I've got some <strong>awesome</strong> articles for you. </p>\n<p>We <em>also</em> have a guide on some <a href=\"https://nodesource.com/blog/nine-fantastic-utilities-for-the-node-js-developer\">great utilities for Node.js developers</a> - if you want to kick your developer experience to 11, be sure to check it out to find some tools to help you get there.</p>\n<p>The goal with this guide was to help kickstart you with <code>package.json</code> for development with Node.js and npm. If you want to take the leap and ensure that you're <em>always</em> on solid footing with Node.js and npm modules, you should check out <a href=\"https://nodesource.com/products/certified-modules/technical-details?utm_campaign=blogref&#x26;utm_source=blog&#x26;utm_content=blog-npmbasics\">NodeSource Certified Modules</a> - an awesome tool to help ensure that you spend more time building applications and less time worrying about modules.</p>\n<div class=\"blog-cta certified-modules\">\n\tLearn more and create your free account \n  <a class=\"button more large\" href=\"https://accounts.nodesource.com/sign-up-blogref/?utm_campaign=blogref&utm_source=blog&utm_content=blog-npmbasics\">Create your NodeSource account</a>\n</div>"}},"categories":[{"name":"How To","slug":"how-to"},{"name":"Node.js","slug":"node-js"}]}},{"node":{"slug":"an-absolute-beginners-guide-to-using-npm","title":"An Absolute Beginner's Guide to Using npm","image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/4WoWwInHFOX8VDxo4SrKEe/e64a0fd1d6b95ff693739264037373ac/18-min.jpg","fileName":"18-min.jpg"}},"bodyContent":{"childMarkdownRemark":{"html":"<p>This article was first published in <a href=\"https://nodesource.com/blog/an-absolute-beginners-guide-to-using-npm/\">NodeSource blog</a> on February 2017.</p>\n<hr>\n<p>Using npm effectively is a cornerstone of modern web development, no matter if it's exclusively with Node.js, as a package manager or build tool for the front-end, or even as a piece of workflows in other languages and on other platforms.</p>\n<p><em>Really</em> understanding npm as a tool, understanding the core concepts, can be something that's difficult for a beginner - I spent many hours just trying to figure out small details that would seem minor or be taken for granted by others. </p>\n<p>As such, I've written up a basic and <strong>detailed</strong> guide for understanding npm, for those who are entirely new to Node.js, npm, and the surrounding ecosystem.</p>\n<h2>An Absolute Beginner's Guide to <code>package.json</code></h2>\n<p>As a general rule, any project that's using Node.js will need to have a <code>package.json</code> file. What is a <code>package.json</code> file?</p>\n<p>At its simplest, a <code>package.json</code> file can be described as a manifest of your project that includes the packages and applications it depends on, information about its unique source control, and specific metadata like the project's name, description, and author.</p>\n<p>Let's break down the core parts of a typical <code>package.json</code> file:</p>\n<h3>Specific Metadata: name, version, description, license, and keywords</h3>\n<p>Inside a package.json, you'll almost always find metadata specific to the project - no matter if it's a web application, Node.js module, or even just a plain JavaScirpt library. This metadata helps identify the project and acts as a baseline for users and contributors to get information about the project.</p>\n<p>Here's an example of how these fields would look in a package.json file:</p>\n<pre><code class=\"language-json\">{\n  \"name\": \"metaverse\", // The name of your project\n  \"version\": \"0.92.12\", // The version of your project\n  \"description\": \"The Metaverse virtual reality. The final outcome of all virtual worlds, augmented reality, and the Internet.\", // The description of your project\n  \"main\": \"index.js\"\n  \"license\": \"MIT\" // The license of your project\n}\n</code></pre>\n<p>A <code>package.json</code> file is <em>always</em> structured in the <a href=\"http://www.json.org/\">JSON</a> format, which allows it to be easily read as metadata and parsed by machines. </p>\n<p>If needing to format a <code>package.json</code> file manually to get your project up and running seems a bit daunting, there's a handy command that will automatically generate a base <code>package.json</code> file for you - if you'd like to learn how to use it, take a peek at the <code>npm init</code> instructions below!</p>\n<h3>Understanding and Managing Your Project's Dependencies: <code>dependencies</code> and <code>devDepenendcies</code> in your <code>package.json</code></h3>\n<p>The other majorly important aspect of a <code>package.json</code> is that it contains a collection of any given project's dependencies. These dependencies are the modules that the project relies on to function properly. </p>\n<p>Having dependencies in your project's <code>package.json</code> allows the project to install the versions of the modules it depends on. By running an install command (see the instructions for <code>npm install</code> below) inside of a project, you can install <em>all</em> of the dependencies that are listed in the project's <code>package.json</code> - meaning they don't have to be (and almost never should be) bundled with the project itself.</p>\n<p>Second, it allows the separation of dependencies that are needed for production and dependencies that are needed for development. In production, you're likely not going to need a tool to watch your CSS files for changes and refresh the app when they change. But in both production and development, you'll want to have the modules that enable what you're trying to accomplish with your project - things like your web framework, API tools, and code utilities.</p>\n<p>What would a project's <code>package.json</code> look like with <code>dependencies</code> and <code>devDependencies</code>? Let's expand on the previous example of a <code>package.json</code> to include some.</p>\n<pre><code class=\"language-json\">{\n  \"name\": \"metaverse\",\n  \"version\": \"0.92.12\",\n  \"description\": \"The Metaverse virtual reality. The final outcome of all virtual worlds, augmented reality, and the Internet.\",\n  \"main\": \"index.js\"\n  \"license\": \"MIT\",\n  \"devDependencies\": {\n    \"mocha\": \"~3.1\",\n    \"native-hello-world\": \"^1.0.0\",\n    \"should\": \"~3.3\",\n    \"sinon\": \"~1.9\"\n  },\n  \"dependencies\": {\n    \"fill-keys\": \"^1.0.2\",\n    \"module-not-found-error\": \"^1.0.0\",\n    \"resolve\": \"~1.1.7\"\n  }\n}\n</code></pre>\n<p>One key difference between the dependencies and the other common parts of a <code>package.json</code> is that they're both objects, with multiple key/value pairs. Every key in both <code>dependencies</code> and <code>devDependencies</code> is a name of a package, and every value is the version range that's acceptable to install (according to Semantic Versioning - to learn more about Semantic Versioning, also known as semver, check out our <a href=\"https://nodesource.com/blog/semver-a-primer/\">primer on semver</a>).</p>\n<div class=\"blog-cta nsolid\" style=\"background-color: #4cb5ff;\">\n\tCan't get enough npm? Download our complete guide: \n  <a class=\"button more large\" href=\"https://pages.nodesource.com/npm-guide-ultimate-wb.html?utm_campaign=blogref&utm_source=blog&utm_content=blog-beginners\">Read now: The Ultimate Guide to npm</a>\n</div>\n<h2>The Essential npm Commands</h2>\n<p>When using npm, you're most likely going to be using the command line tool for the majority of your interactions. As such, here's a detailed rundown of the commands that you'll encounter and need to use most frequently.</p>\n<h2>Using <code>npm init</code> to Initialize a Project</h2>\n<p>The <code>npm init</code> command is a step-by-step tool to scaffold out your project. It will prompt you for input for a few aspects of the project in the following order:</p>\n<ul>\n<li>The project's name,</li>\n<li>The project's initial version,</li>\n<li>The project's description,</li>\n<li>The project's entry point (meaning the project's main file),</li>\n<li>The project's test command (to trigger testing with something like <a href=\"https://github.com/feross/standard\">Standard</a>)</li>\n<li>The project's git repository (where the project source can be found)</li>\n<li>The project's keywords (basically, tags related to the project)</li>\n<li>The project's license (this defaults to ISC - most open-source Node.js projects are MIT)</li>\n</ul>\n<p>It's worth noting that if you're content with the <em>suggestion</em> that the <code>npm init</code> command provides next to the prompt, you can simply hit <code>Return</code> or <code>Enter</code> to accept the suggestion and move on to the next prompt.</p>\n<p>Once you run through the <code>npm init</code> steps above, a <code>package.json</code> file will be generated and placed in the current directory. If you run it in a directory that's not exclusively for your project, don't worry! Generating a <code>package.json</code> doesn't really <em>do</em> anything, other than create a <code>package.json</code> file. You can either move the <code>package.json</code> file to a directory that's dedicated to your project, <em>or</em> you can create an entirely new one in such a directory.</p>\n<h4>How to use <code>npm init</code>:</h4>\n<pre><code>npm init # This will trigger the initialization\n</code></pre>\n<h3>Using <code>npm init --yes</code> to <em>Instantly</em> Initialize a Project</h3>\n<p>If you want to get on to building your project, and don't want to spend the (albeit brief) time answering the prompts that come from <code>npm init</code>, you can use the <code>--yes</code> flag on the <code>npm init</code> command to automatically populate all options with the default <code>npm init</code> values.</p>\n<blockquote>\n<p><strong>Note:</strong> You can configure what these default values are with the npm configuration - that's a more advanced topic, and outside the scope of this beginner's guide to npm. </p>\n<p>That said, if you're interested in setting that up, you can learn how to set these defaults in the eleventh tip of our <a href=\"https://nodesource.com/blog/eleven-npm-tricks-that-will-knock-your-wombat-socks-off\">npm tricks</a> article.</p>\n</blockquote>\n<h4>Usage:</h4>\n<pre><code>npm init --yes # This will trigger automatically populated initialization.\n</code></pre>\n<h2>Install Modules with <code>npm install</code></h2>\n<p>Installing modules from npm is one of the most basic things you should learn to do when getting started with npm.  As you dive deeper, you'll begin to learn some variations on installing modules, but here's the very core of what you need to know to install a standalone module into the current directory:</p>\n<pre><code>npm install &#x3C;module>\n</code></pre>\n<p>In the above command, you'd replace <code>&#x3C;module></code> with the name of the module you want to install. For example, if you want to install Express (the most used and most well known Node.js web framework), you could run the following command:</p>\n<pre><code>npm install express\n</code></pre>\n<p>The above command will install the <code>express</code> module into <code>/node_modules</code> in the current directory. Whenever you install a module from npm, it will be installed <em>into</em> the <code>node_modules</code> folder.</p>\n<p>In addition to triggering an install of a single module, you can actually trigger the installation of <em>all</em> modules that are listed as <code>dependencies</code> and <code>devDependencies</code> in the <code>package.json</code> in the current directory. To do so, you'll simply need to run the command itself:</p>\n<pre><code>npm install\n</code></pre>\n<p>Once you run this, npm will begin the installation process of all of the current project's dependencies. </p>\n<p>As an aside, one thing to note is that there's an alias for <code>npm install</code> that you may see in the wild when working with modules from the ecosystem. The alias is <code>npm i</code>, where <code>i</code> takes the place of <code>install</code>. </p>\n<p>This seemingly minor alias is a small gotcha for beginners - including myself, several times when I was learning - to the Node.js and npm ecosystems, as there's not a standardized, single way that module creators and maintainers will instruct on how to install their module.</p>\n<h4>Usage:</h4>\n<pre><code class=\"language-bash\">npm install &#x3C;module> # Where &#x3C;module> is the name of the module you want to install\nnpm i &#x3C;module> # Where &#x3C;module> is the name of the module you want to install - using the i alias for installation\n</code></pre>\n<h3>Install modules and save them to your <code>package.json</code> as a dependency</h3>\n<p>As with <code>npm init</code>, the <code>npm install</code> command has a flag or two that you'll find useful in your workflow - it'll save you time and effort with regard to your project's <code>package.json</code> file.</p>\n<p>When you're running <code>npm install</code> to install a module, you can add the optional flag <code>--save</code> to the command. This flag will add the module as a dependency of your project to the project's <code>package.json</code> as an entry in <code>dependencies</code>. </p>\n<h4>Usage:</h4>\n<pre><code>npm install &#x3C;module> --save # Where &#x3C;module> is the name of the module you want to install\n</code></pre>\n<h3>Install Modules and Save Them to Your <code>package.json</code> as a Developer dependency</h3>\n<p>There's a flag that is nearly an exact duplicate, in terms of functionality, of the <code>--save</code> flag when installing a module: <code>--save-dev</code>. There are a few a key differences between the two - instead of saving the module being installed and added to <code>package.json</code> as an entry in <code>dependencies</code>, it will save it as an entry in the <code>devDependencies</code>. </p>\n<p>The semantic difference here is that <code>dependencies</code> are for use in production - whatever that would entail for your project. On the other hand, <code>devDependencies</code> are a collection of the dependencies that are used in <em>development</em> of your application - the modules that you use to build it, but don't need to use when it's <em>running</em>. This could include things like testing tools, a local server to speed up your development, and more.</p>\n<h4>Usage:</h4>\n<pre><code>npm install &#x3C;module> --save-dev # Where &#x3C;module> is the name of the module you want to install\n</code></pre>\n<h3>Install Modules Globally on your System</h3>\n<p>The final, and most common, flag for <code>npm install</code> that you should are the flags to install a module globally on your system. </p>\n<p>Global modules can be extremely useful - there are tons tools, utilities, and more for both development and general usage that you can install globally to use. </p>\n<p>To install a module from npm globally, you'll simply need to use the <code>--global</code> flag when running the install command to have the module install globally, rather than locally (to the current directory).</p>\n<blockquote>\n<p><strong>Note:</strong> One caveat with global modules is that, by default, npm will install them to a system directory, not a local one. With this as the default, you'll need to authenticate as a privileged user on your system to install global modules.</p>\n<p>As a best practice, you should change the default installation location from a system directory to a user directory. If you'd like to learn to do this, take a peek at the seventh tip in our <a href=\"https://nodesource.com/blog/eleven-npm-tricks-that-will-knock-your-wombat-socks-off\">npm tricks</a> article!</p>\n</blockquote>\n<h4>Usage:</h4>\n<pre><code class=\"language-bash\">npm install &#x3C;module> --global # Where &#x3C;module> is the name of the module you want to install globally\nnpm install &#x3C;module> -g # Where &#x3C;module> is the name of the module you want to install globally, using the -g alias\n</code></pre>\n<h2>Want to keep going?</h2>\n<p>If you want to keep learning about npm and all its facets, I've got a few awesome things for you. A bit ago, we shared a few <a href=\"https://nodesource.com/blog/eleven-npm-tricks-that-will-knock-your-wombat-socks-off\">npm tricks to knock your wombat socks off</a>. Even better, we wrote a follow-up with <a href=\"https://nodesource.com/blog/seven-more-npm-tricks-to-knock-your-wombat-socks-off\">even more npm tricks</a>! This beginner's guide is a great springboard to get off the ground, and both of those will help you start optimizing your work with npm! If you'd like to go even further with npm and start deploying Node.js apps and npm modules into production, you should <em>definitely</em> take a look at <a href=\"https://certified.nodesource.com/\">NodeSource Certified Modules</a> - it's an awesome tool that'll compliment your newly acquired npm skills!</p>"}},"categories":[{"name":"How To","slug":"how-to"},{"name":"Tutorials","slug":"tutorials"},{"name":"Node.js","slug":"node-js"}]}}]},"contentfulHero":{"headLine":"List of useful articles and blog post on Node.js","coverImage":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/3tCG630sR3116gwqT52u9C/dadf240e71f8ab06e2492da2e46a561a/blog-min.jpg","fileName":"blog-min.jpg"}}}},"pageContext":{"limit":7,"skip":21,"numPages":5,"currentPage":4,"type":"blog","hero":"hero-blogs","prevPath":"/blogs/page/3","nextPath":"/blogs/page/5"}}}