Nodejs Interview Questions

We offer you a brighter future with FREE online courses - Start Now!!

This is the first part of the interview questions articles. Here you will find the most important questions that are asked in node js interview. For each question a detailed and efficient answer is provided. This article is beginner friendly containing the basics of nodejs.

Nodejs Interview Questions

1. What is npm about?
Ans. It is the most popularly used package manager. It installs different packages available for node.js.
Npm is a package manager for node.js which installs available packages into our project. It is a central repository for all available packages.

2. Is npm better than any other package manager?
Ans. Yes.

  • Manages global and local project tools.
  • It is a very large repository. This contains everything which we will require to make a project.
  • It helps to switch versions of our tool.
  • This provides package-lock.json and package.json

3. How can you start building a nodejs project?
Ans.

  • To initialize a new project, we open a terminal in the project directory and run npm init.
  • We can keep everything to default or answer according to the questions asked.

4.What is the main difference between installing a package globally as compared to installing it locally?
Ans. Local packages are present in the project directory, whereas global packages are kept in a single folder in your system
The local packages are not accessible outside the project whereas you can use the global packages in any project.

5. What are the contents present in the package.json file?
Ans.

  • Name-Indicates package name
  • Version- Indicates version of package
  • Description- About the package.
  • Author- Package author
  • Main- Entry point for the package
  • Keywords, Contributors, Dependencies

6. What are dependencies?
Ans. Dependencies provide information regarding the packages we use in the project.

All the packages shown in the dependencies property are required to run the node js application.

It contains details about the package like version number, etc.

7. What is devdependencies ?
Ans. Devdependencies are very much similar with dependencies but the main difference is that the packages which are under the devdependency are necessary only during the development of our application.

We don’t require these dependencies when our application is in production.

8. What can you say about npm audit?
Ans. We install a lot of packages in our system and many times we unknowingly install harmful packages, so to identify these malicious packages npm audit is used. In order to fix the vulnerabilities, the npm audit fix command is run.

9. What do you understand about npm publish?
Ans. If you want your own modules to be available in https://www.npmjs.com/ than you need to publish them using the “npm publish” command. Once it is published everybody will be able to install it using the “npm install <your package name>” command.

10. What are core modules in nodejs?
Ans. Modules that are part of nodejs and installed automatically with the Node.js installation process are called core modules.

To include this module in our program we use the require function.

Eg: let name= require(‘name’)

The return type of require() function depends on what the module returns.

11. What do you mean by Local module?
Ans. Modules that are created by us locally are local modules. Local modules are imported in our program in the same way as we include the built in module.

The Exports keyword is used to make methods available in our module to outside the file.

In order to include this method/function in our file, we use the require function.

12. Describe the http methods.
Ans.

  • http.METHODS: Provides all the methods of http module.
  • http.STATUS_CODES: Provides all the status codes with its description.
  • CREATE SERVER: It takes a function which runs when we try to access the port.
  • listen() : waits for connections.

13. Describe the methods of make file?
Ans. Below are the different methods of make file:

  • Append file
    The fs.appendFile() method takes the text that we want to append then it appends at the end of the file, if the file does not exist then the file will be created.
  • Open file
    The fs.open() method argument is “w” for “writing” indicates that the file is open for writing
  • WriteFile:
    It takes the text that we want to write then it overrides the content available in the file

14. What are the important modules of nodejs?
Ans. Below are the important modules of nodejs:

  • Http: Used for creating an HTTP server.
  • Assert: Used for testing using a set of assertion functions.
  • Fs: Used in modification of files.
  • Path: Find the file paths.
  • Process: Gives information regarding the current process.
  • Querystring: For parsing and proper formatting of the url.
  • Url: For parsing the urls.

15. How can you modify a file?
Ans. The fs.appendFile() method takes the text that we want to append, then it appends at the end of the file. If the file does not exist then the file will be created.

fs.writeFile() is for replacing the content of the file.

Summary:

This was all about nodejs interview questions and answers. It covers most important interview questions which are common in a nodejs interview.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *