Why is __dirname not defined in node REPL?

后端 未结 11 1501
傲寒
傲寒 2020-12-12 14:23

From the node manual I see that I can get the directory of a file with __dirname, but from the REPL this seems to be undefined. Is this a misunderstanding on my

11条回答
  •  没有蜡笔的小新
    2020-12-12 15:26

    I was also trying to join my path using path.join(__dirname, 'access.log') but it was throwing the same error.

    Here is how I fixed it:

    I first imported the path package and declared a variable named __dirname, then called the resolve path method.

    In CommonJS

    var path = require("path");
    
    var __dirname = path.resolve();
    

    In ES6+

    import path  from 'path';
    
    const __dirname = path.resolve();
    

    Happy coding.......

提交回复
热议问题