0
0
Node.jsframework~10 mins

path.resolve for absolute paths 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 path module.

Node.js
const path = require('[1]');
Drag options to blanks, or click blank then click option'
Apath
Bfs
Chttp
Durl
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fs' instead of 'path' to import the path module.
Forgetting to import the module before using it.
2fill in blank
medium

Complete the code to get the absolute path of 'file.txt' in the current directory.

Node.js
const absolutePath = path.[1]('file.txt');
Drag options to blanks, or click blank then click option'
Aresolve
Bdirname
Cbasename
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using path.join which does not guarantee an absolute path.
Using path.basename which returns only the file name.
3fill in blank
hard

Fix the error in the code to correctly resolve the absolute path of 'data.json' inside the 'config' folder.

Node.js
const configPath = path.resolve(__dirname, [1]);
Drag options to blanks, or click blank then click option'
A"config/data.json"
Bconfig/data.json
C'config\data.json'
D'config/data.json'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using quotes around the path string causing a syntax error.
Using backslashes without escaping them in strings.
4fill in blank
hard

Fill both blanks to create an absolute path to 'index.js' inside the 'src' folder, starting from the current directory.

Node.js
const indexPath = path.[1](__dirname, [2]);
Drag options to blanks, or click blank then click option'
Aresolve
Bjoin
C'src/index.js'
D'src\index.js'
Attempts:
3 left
💡 Hint
Common Mistakes
Using path.join which may not return an absolute path.
Using backslashes without escaping in the path string.
5fill in blank
hard

Fill all three blanks to create an absolute path to 'app.js' inside the 'lib' folder, using path.resolve and process.cwd().

Node.js
const appPath = path.[1](process.[2](), [3]);
Drag options to blanks, or click blank then click option'
Aresolve
Bcwd
C'lib/app.js'
D'lib\\app.js'
Attempts:
3 left
💡 Hint
Common Mistakes
Using __dirname instead of process.cwd() when the current working directory is needed.
Not quoting the path string or using backslashes incorrectly.