![]() ![]() Or you can directly use the command npm init -y (same as pressing enter to all the questions). Then press enter or answer the questions accordingly. To initiate NPM in our program, we can use the following command: npm init You can read more about the chalk package here. To install the chalk package (we will install version 2.4.2 as it allows us to import the package using the require method), type the following command: npm install to style the text string, use this command to pick the color of the string: lor(text) Now we’ll install one of the most used npm packages called “chalk” - it styles text strings in the terminal. To display the name of a random superhero, use this command: console.log(sh.random()). Now to import the installed package type const sh = require(“superheroes”). To install any npm package, we can use this command in the cmd: npm install superheroes ![]() Now we will use a very basic npm package called superheroes (which is a list of random superheroes) to help you understand how npm works. To append anything in the same file we can use this: fs.appendFileSync(filename, content). const fs = require(“fs”) įs.writeFileSync(“file.txt”, “Hi there.”) To create a file, we can use fs.writeFileSync(filename, content). Now to use any function of this module, you can refer to the docs. To import the fs module, type this command: const fs = require(“fs”). You use it to create, read, and modify files. So let’s start with the very basic package, that is fs (file system). Open the terminal, and navigate to the file location. Type the code console.log(“Hello World”) in your editor. Then open the file in your code editor like VS Code. How to Write Your First Node.js Program (Hello World)Ĭreate a file called hello_world.js in your project folder NPX stands for Node Package Execute, and it can execute any npm package without even installing it. ![]() NPM stands for Node Package Manager which help you manage your packages for Node. This makes Node extraordinarily fast and efficient.īy event-driven, it means that as soon as Node starts it initiates all the variables and functions and waits for an event to occur. This means it's asynchronous, and doesn't block itself for one request (but rather immediately moves to the next request). Node is an event-driven, non-blocking IO model. It is built on Chrome’s V8 JavaScript Engine. Node.js is a JavaScript runtime that extends its capability to the server-side. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |