In Node.js how can I tell the path of `this` module?

后端 未结 1 624
一生所求
一生所求 2020-12-29 23:45

In a Node.js module I\'m writing I would like to open a file--i.e, with fs.readFile()--that is contained in the same directory as my module. By which I mean it

相关标签:
1条回答
  • 2020-12-30 00:16

    As david van brink mentioned in the comments, the correct solution is to use __dirname. This global variable will return the path of the currently executing script (i.e. you might need to use ../ to reach the root of your module).

    For example:

    var path = require("path");
    require(path.join(__dirname, '/models'));
    

    Just to save someone from a headache.

    0 讨论(0)
提交回复
热议问题