Open In App

What command is required for external libraries to be imported ?

Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework and it’s not a programming language.

External Libraries are pre-written sections of code that are simple to use. In order to carry out common JavaScript functions and libraries are sets of predefined code fragments that may be accessed and reused. The code of a specific JavaScript library can be plugged in and out of the rest of your project’s code as necessary. 



Some of the external libraries used in JavaScript are:

Problem Statement: State the command which is required for importing external libraries in Node.js Also discuss the approach and code with its implementation.



Syntax:

var http=require ("HTTP")
npm install library_name

Approach 1: Calling the ‘require’ function for importing external libraries

Example 1: In this example, we’ll see how to import the mathjs library:

Note: mathjs is an external library that does not comes with the initial node module. So, we need to install mathjs first using the npm command before using:

npm i mathjs




const math = require ('mathjs')
 
let library = math.sqrt(-4);
console.log (library);

Output: 

Complex { re: 0, im: 2 }

Example 2:




const math = require('mathjs')
 
let library = math.evaluate('1.2 * (2 + 4.5)')
console.log(library);

Output:

7.8

Approach 2: Installing the external packages.

Example: Let us install the React library by installing React Library as follows:

Just type the following code in your terminal and it will install React in your local system:

npx create-react-app filename

After installation, your code will contain the following files:

 

Step to run the application: Write the below code in the terminal to run the application:

npm start

Output:

 

Note: The above output is showing the default page of React application.


Article Tags :