Project Setup & Modules Flashcards

1
Q

How to setup a Node.js project?

A
  1. Install Node.js
  2. Create a directory for the project.
  3. Open a terminal and run npm init -y (-y flags to use default settings)
  4. Create a file app.js
  5. Run Node.js application using node app.js
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is NPM? What is the role of node_modules folder?

A

NPM is a tool that help us manage dependencies of our project.

node_modules folder contains all the dependencies of the Node.js app. These dependencies are basically libraries or modules that other people built.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the role of package.json file in Node?

A

It is a central manifest for Node.js projects.

Contains the project metadata (information about the project).

It also contains the dependencies of the project that are installed when running npm install and we can configure some aliases for scripts we want to run.

Lastly, it also contains the project configuration, such as type, which node version, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are modules in Node? What is the difference between a function & module?

A

A module contains specific functionality that can be reused within a Node.js app. Ideally, each JavaScript file is a single module.

The difference between functions and modules is that functions are a set of instructions with a strict syntax, while module encapsulates functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How many ways are there to Export a module?

A

Using module.exports (CommonJS syntax)

module.exports.myFunction = myFunction;
module.exports.aConst = aConst;
module.exports.myObject = myObject;

Directly using the exports object (CommonJS syntax)

exports.sayHello = function(name){
console.log(“Hello, “, name);
}

Using the keyword export (ES Modules syntax).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Import syntax

A

CommonJS: const { myFunction } = require(‘./myModule’)

ES Modules: import { myFunction } from ‘./myModule’

Loading Behavior:

CommonJS loads modules synchronously and can be dynamic

ES Modules are statically analyzed at parse time and support asynchronous loading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What happens if you don’t export the module?

A

It won’t be available to be imported in other modules/files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is module wrapper function?

A

Everything we write inside a .js file get’s automatically converted/wrapped by Node into a IIFE before it is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the types of modules in Node?

A

Built-in Modules (Core Modules)
Local Modules (User defined modules)
Third-Party Modules (Libraries, external packages);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly