Skip to content

Instantly share code, notes, and snippets.

@antonlvovych
Last active March 6, 2022 23:16
Show Gist options
  • Save antonlvovych/e2e3e0325abd9023d3a3 to your computer and use it in GitHub Desktop.
Save antonlvovych/e2e3e0325abd9023d3a3 to your computer and use it in GitHub Desktop.
Get root path of node.js application
var path = require('path');
module.exports = (function () {
return path.dirname(require.main.filename || process.mainModule.filename);
})();
// Example of usage:
var root = require('root'); // In root will be absolute path to your application
@gssantost
Copy link

Useful!! Thanks

@imvikaskohli
Copy link

Very useful! Thanks a lot

@glarivie
Copy link

this is not working

@harsha-iiiv
Copy link

Hey @glarivie try this path.resolve(process.cwd(), 'your filename in root folder')

@Makhsudjon
Copy link

This function gives only root folder of current file not root of application.

@sasial-dev
Copy link

Use process.cwd()

@inexas
Copy link

inexas commented Jul 29, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment