Open In App

What command is required for external libraries to be imported ?

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Mathematical Libraries
  • React Library
  • jQuery Library
  • Underscore Library
  • jQueryUI

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:

  • We need to use the “require” internal modules.
var http=require ("HTTP")
  • or “npm install” command for importing third-party or external libraries.
npm install library_name

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

  • The built-in NodeJS method require() to be used to incorporate external modules that are included in different files. 
  • The require() statement essentially reads and executes a JavaScript file before returning the export object. 
  • The NodeJS require() statement also enables the addition of locally developed and community-based modules. Installing packages that your application can utilize is possible with Node.js’s Node package manager (NPM). 
  • Express is available with NPM, a widely used and efficient framework for loading web applications. 

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

Javascript




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


Output: 

Complex { re: 0, im: 2 }

Example 2:

Javascript




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.

  • We can use or include the external libraries in our terminal and then we can get the output in our browser by this method.
  • All libraries that the web part module depends on are by default automatically included by the web part bundler. 
  • This indicates that your web part’s JavaScript bundle file and the library’s JavaScript bundle file are both deployed.

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads