EN
TypeScript / Node.js - get current working directory
3 points
In this article, we would like to show you how to get the current working directory in Node.js.
Quick solution:
xxxxxxxxxx
1
const directory = process.cwd();
or:
xxxxxxxxxx
1
import * as path from 'path';
2
3
const directory: string = path.resolve();
In this example, we create two variables for the current working directory using two different methods.
xxxxxxxxxx
1
import * as path from 'path';
2
3
const directory1: string = process.cwd();
4
const directory2: string = path.resolve();
5
6
console.log(directory1); // C:\Project
7
console.log(directory2); // C:\Project
Output:
xxxxxxxxxx
1
C:\Project
2
C:\Project