Get application full path in Node.js -
i'm wondering if there best way or best practice full path of application in node.js. example: have a module in sub folder /apps/myapp/data/models/mymodel.js, , full path of app (not full path of file), return me /apps/myapp, how do that? know _dirname or _file relative file not full path of app.
there's better solution, but should work:
var path = require('path'); // find first module loaded var topmodule = module; while(topmodule.parent) topmodule = topmodule.parent; var appdir = path.dirname(topmodule.filename); console.log(appdir); edit: andreas proposed better solution in comments:
path.dirname(require.main.filename) edit: solution nam nguyen
path.dirname(process.mainmodule.filename)
Comments
Post a Comment