javascript - In Node.js how can I tell the path of `this` module? -
in node.js module i'm writing open file--i.e, fs.readfile()
--that contained in same directory module. mean in same directory ./node_modules/<module_name>/index.js
file.
it looks relative path operations performed fs
module take place relative directory in node.js started. such, think need know how path of current node.js module executing.
thanks.
as david van brink mentioned in comments, correct solution use __dirname
. global variable return path of executing script (i.e. might need use ../
reach root of module).
for example:
var path = require("path"); require(path.join(__dirname, '/models'));
just save headache.
Comments
Post a Comment