0
0
Node.jsframework~10 mins

process.cwd and __dirname 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 print the current working directory using Node.js.

Node.js
console.log([1]());
Drag options to blanks, or click blank then click option'
Aprocess.chdir
B__dirname
Cprocess.cwd
Drequire
Attempts:
3 left
💡 Hint
Common Mistakes
Using __dirname instead of process.cwd()
Trying to call process.chdir() which changes directory
Using require which is for modules
2fill in blank
medium

Complete the code to print the directory name of the current module file.

Node.js
console.log([1]);
Drag options to blanks, or click blank then click option'
A__dirname
Bmodule.filename
Cprocess.cwd()
Dprocess.env.PWD
Attempts:
3 left
💡 Hint
Common Mistakes
Using process.cwd() which is the working directory, not the file's directory
Trying to call __dirname as a function
Using module.filename which gives the full file path
3fill in blank
hard

Fix the error in the code to correctly print the current working directory.

Node.js
console.log(process.[1]);
Drag options to blanks, or click blank then click option'
Acwd()
Bcwd[]
Ccwd
Dcwd{}
Attempts:
3 left
💡 Hint
Common Mistakes
Using process.cwd without parentheses
Trying to access cwd as an array or object
4fill in blank
hard

Fill both blanks to create an absolute path to a file named 'data.txt' in the current module's directory.

Node.js
const path = require('path');
const filePath = path.join([1], [2]);
console.log(filePath);
Drag options to blanks, or click blank then click option'
A__dirname
B'data.txt'
Cprocess.cwd()
D'./data.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using process.cwd() instead of __dirname for the directory
Using './data.txt' which is a relative path and may not resolve correctly
5fill in blank
hard

Fill both blanks to create a function that returns the absolute path of a file given its name, relative to the current module's directory.

Node.js
const path = require('path');
function getFilePath(filename) {
  return path.join([1], [2], filename,);
}
Drag options to blanks, or click blank then click option'
A__dirname
B'src'
C,
D'/'
Attempts:
3 left
💡 Hint
Common Mistakes
Using slashes '/' inside path.join arguments instead of separate arguments
Forgetting commas between arguments
Using process.cwd() instead of __dirname