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

后端 未结 4 647
谎友^
谎友^ 2020-11-29 14:54

What\'s the difference between

console.log(process.cwd())

and

console.log(__dirname);

I\'ve seen both use

4条回答
  •  独厮守ぢ
    2020-11-29 15:34

    Knowing the scope of each can make things easier to remember.

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

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

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

提交回复
热议问题