일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
- API
- solidity
- REACT
- CLASS
- node.js
- evm
- typeScript
- concept
- Interface
- bitcoin
- 기준
- tailwindcss
- CSS
- Redux
- hardhat
- HTML
- useState
- middleware
- built in object
- blockchain
- Props
- 삶
- SSR
- web
- Ethereum
- express.js
- error
- JavaScript
- graphQL
- nextJS
- Today
- Total
ReasonJun
Node.js : __dirname / __filename / process.cwd() 본문
In Node.js, the following three variables are used to get the current working directory and the filename of the current module:
- __dirname
- __filename
- process.cwd()
__dirname is a special variable that is defined within each module. It returns the directory name of the current module, which is the directory that contains the current JavaScript file. For example, if the current file is /home/user/my_app/index.js, then __dirname will be /home/user/my_app.
__filename is another special variable that is defined within each module. It returns the filename of the current module, including the extension. For example, if the current file is /home/user/my_app/index.js, then __filename will be index.js.
process.cwd() is a method of the global process object. It returns the current working directory of the Node.js process. The current working directory is the directory that the Node.js process was started from.
The main difference between __dirname and process.cwd() is that __dirname is specific to the current module, while process.cwd() is global. This means that __dirname will always return the directory name of the current module, even if the module is loaded from a different directory. However, process.cwd() will return the current working directory of the Node.js process, which may be different from the directory that the current module is located in.
Here is an example of how to use __dirname and process.cwd() in Node.js:
const dirname = __dirname;
const filename = __filename;
const cwd = process.cwd();
console.log("__dirname:", dirname);
console.log("__filename:", filename);
console.log("process.cwd():", cwd);
This code will print the following output:
__dirname: /home/user/my_app
__filename: index.js
process.cwd(): /home/user
As you can see, __dirname is /home/user/my_app, __filename is index.js, and process.cwd() is /home/user. This is because the current module is index.js, which is located in the /home/user/my_app directory. The Node.js process was also started from the /home/user directory.
__dirname / __filename / process.cwd()
'Backend > Node.js' 카테고리의 다른 글
Node.js : Blocking / non-Blocking (0) | 2023.09.03 |
---|---|
Node.js : bindings (0) | 2023.09.02 |
In Node.js, the relationship between V8 and libuv (0) | 2023.09.02 |
Node.js : libuv concept (0) | 2023.09.02 |
Node.js : Concept and Features (0) | 2023.09.02 |