node.js process.cwd() 与 __dirname 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9874382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:23:33  来源:igfitidea点击:

What's the difference between process.cwd() vs __dirname?

node.js

提问by helpermethod

What's the difference between

有什么区别

console.log(process.cwd())

and

console.log(__dirname);

I've seen both used in similar contexts.

我见过两者都在类似的情况下使用。

回答by Raynos

process.cwd()returns the current working directory,

process.cwd()返回当前工作目录,

i.e. the directory from which you invoked the nodecommand.

即您从中调用node命令的目录。

__dirnamereturns the directory name of the directory containing the JavaScript source code file

__dirname返回包含 JavaScript 源代码文件的目录的目录名

回答by samuelj90

As per node js docprocess.cwd()

根据节点js文档process.cwd()

cwdis a method of global object process, returns a string value which is the current working directory of the Node.js process.

cwd是一个全局对象的方法process,返回一个字符串值,它是 Node.js 进程的当前工作目录。

As per node js doc__dirname

根据节点js文档__dirname

The directory name of current script as a string value. __dirname is not actually a global but rather local to each module.

当前脚本的目录名称作为字符串值。__dirname 实际上不是全局的,而是每个模块的局部变量。

Let me explain with example,

让我用例子来解释,

suppose we have a main.jsfile resides inside C:/Project/main.jsand running node main.jsboth these values return same file

假设我们有一个main.js文件驻留在里面C:/Project/main.js并运行node main.js这两个值返回相同的文件

or simply with following folder structure

或者简单地使用以下文件夹结构

Project 
├── main.js
└──lib
   └── script.js

main.js

主文件

console.log(process.cwd())
// C:\Project
console.log(__dirname)
// C:\Project
console.log(__dirname===process.cwd())
// true

suppose we have another file script.jsfiles inside a sub directory of project ie C:/Project/lib/script.jsand running node main.jswhich require script.js

假设我们script.js在项目 ie 的子目录中有另一个文件文件C:/Project/lib/script.js并且正在运行node main.js它需要script.js

main.js

主文件

require('./lib/script.js')
console.log(process.cwd())
// C:\Project
console.log(__dirname)
// C:\Project
console.log(__dirname===process.cwd())
// true

script.js

脚本.js

console.log(process.cwd())
// C:\Project
console.log(__dirname)
// C:\Project\lib
console.log(__dirname===process.cwd())
// false

回答by themefield

Knowing the scopeof each can make things easier to remember.

了解每个的范围可以使事情更容易记住。

processis node's global object, and .cwd()returns where node is running.

processisnode的全局对象,并.cwd()返回节点运行的位置。

__dirnameis module's property, and represents the file path of the module. In node, one module resides in one file.

__dirnameismodule的属性,表示模块的文件路径。在节点中,一个模块驻留在一个文件中。

Similarly, __filenameis another module's property, which holds the file name of the module.

同样,__filename是 anothermodule的属性,它保存模块的文件名。

回答by user1412192

$ find proj

$ find proj

proj
proj/src
proj/src/index.js

$ cat proj/src/index.js

$ cat proj/src/index.js

console.log("process.cwd() = " + process.cwd());
console.log("__dirname = " + __dirname);

$ cd proj; node src/index.js

$ cd proj; node src/index.js

process.cwd() = /tmp/proj
__dirname = /tmp/proj/src