0
0
Node.jsframework~10 mins

CommonJS require and module.exports in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the 'fs' module using CommonJS require.

Node.js
const fs = [1]('fs');
Drag options to blanks, or click blank then click option'
Arequire
Bimport
Cinclude
Dload
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'import' instead of 'require'
Using 'include' or 'load' which are not valid in CommonJS
2fill in blank
medium

Complete the code to export a function named 'greet' using module.exports.

Node.js
module.exports = [1];
Drag options to blanks, or click blank then click option'
Agreet()
Bgreet
C{ greet }
Dfunction greet()
Attempts:
3 left
💡 Hint
Common Mistakes
Using greet() which calls the function instead of exporting it
Using braces which exports an object, not the function itself
3fill in blank
hard

Fix the error in importing the default export from 'math.js' using require.

Node.js
const add = [1]('./math.js');
Drag options to blanks, or click blank then click option'
Afetch
Bimport
Crequire
Dinclude
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'import' which is ES Modules syntax
Using 'fetch' or 'include' which are invalid here
4fill in blank
hard

Fill both blanks to export an object with two functions, 'start' and 'stop'.

Node.js
module.exports = { [1], [2] };
Drag options to blanks, or click blank then click option'
Astart
Bstop
Cbegin
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function names that do not exist
Forgetting to export both functions
5fill in blank
hard

Fill all three blanks to import 'readFile', export a function 'writeFile', and assign it to module.exports.

Node.js
const { [1] } = require('fs');

function [2](path, data) {
  // write data to file
}

module.exports = [3];
Drag options to blanks, or click blank then click option'
AreadFile
BwriteFile
DreadFileSync
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up imported and exported function names
Not exporting the function correctly