✅ 1. Core Node.js Concepts - Modules Flashcards

CommonJS vs ES6 modules, `require()` vs `import`

1
Q

What is the syntax for importing named exports in ES6?

A

import { myFunction } from ‘./myModule.mjs’;

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

What is the default module system in Node.js?

A

CommonJS (require() and module.exports) is the default module system in Node.js.

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

What is the difference between CommonJS and ES6 modules?

A
  • CommonJS: Uses require() and module.exports.
  • ES6: Uses import and export.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What file extension is required for ES6 modules in Node.js?

A

.mjs or add “type”: “module” in package.json.

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

How do you export multiple functions in CommonJS?

A

module.exports = {
add,
subtract
};

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

How do you export multiple functions in ES6 modules?

A

export { add, subtract };

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

What is the output of this CommonJS code?
const math = require(‘./math’);
console.log(math.add(2, 3));

A

The output is the result of math.add() if the math module exports an add function.

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

What is the output of this ES6 module code?
import { add } from ‘./math.mjs’;
console.log(add(2, 3));

A

The output is 5 if the add function is properly exported.

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

What is the difference between require() and import in terms of execution?

A
  • require() is synchronous.
  • import is asynchronous.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you import a default export in ES6 modules?

A

import myModule from ‘./myModule.mjs’;

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

How do you import a default export in CommonJS?

A

const myModule = require(‘./myModule’);

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

What is the difference between module.exports and exports in CommonJS?

A
  • module.exports: The actual exported object.
  • exports: A shortcut reference to module.exports.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is tree-shaking in ES6 modules?

A

It is the process of removing unused exports during bundling to optimize the code.

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

What is the output of this code?
console.log(module);

A

It displays the current module object with properties like exports, id, and filename.

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

How do you dynamically import a module in ES6?

A

const module = await import(‘./math.mjs’);

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

What is the benefit of using ES6 modules over CommonJS?

A
  • Asynchronous loading.
  • Static analysis for better optimization.
  • Tree-shaking support.
17
Q

What is the output of this ES6 code?
export default function greet() {
console.log(‘Hello’);
}

A

It exports the greet function as the default export.

18
Q

What happens if you reassign exports in CommonJS?

A

It breaks the module export. Use module.exports instead:
exports = { name: ‘test’ }; // Invalid
module.exports = { name: ‘test’ }; // Correct

19
Q

How do you use import syntax with CommonJS modules?

A

You cannot directly use import with CommonJS unless you add “type”: “module” in package.json.

20
Q

What is the output of this code?
module.exports = () => console.log(‘Hello’);

A

Exports an anonymous function that logs Hello.

21
Q

How do you handle circular dependencies in Node.js modules?

A
  • Refactor code to avoid circular references.
  • Use lazy loading or import().
22
Q

What is the output of this code using CommonJS?
const myModule = require(‘./myModule’);
console.log(myModule);

A

Logs the exported object or function from myModule.

23
Q

What is the output of this code using ES6?
import * as myModule from ‘./myModule.mjs’;
console.log(myModule);

A

Logs an object with all named exports from myModule.

24
Q

How do you mix import and require() in the same file?

A

You cannot mix them. Use either CommonJS or ES6 module syntax.

25
What is the benefit of using import() over require()?
import() is asynchronous and supports dynamic loading.
26
How do you mark a project as ES6 modules?
Add the following in package.json: json "type": "module"
27
What is the output of this code? const myModule = require('./myModule'); console.log(typeof myModule);
The output is the data type of the exported value.
28
How do you handle backward compatibility between CommonJS and ES6 modules?
- Use Babel or TypeScript for compatibility. - Use .mjs for ES6 and .cjs for CommonJS files.
29
What is the output of this code? import { add } from './math.mjs'; console.log(typeof add);
Logs the data type of the add export.
30
How do you import everything from a module in ES6?
import * as utils from './utils.mjs';