Using a root directory in the Nodejs include

Asked

Viewed 1,024 times

3

I am learning Nodejs and I started to create a project just to get used to developing with him, however, I am bothered by the amount of ../../../../../ used when including a code. How can I use my application’s root folder as path reference?

1 answer

4


__dirname

This gives you the currently running file path

__filename

This is the absolute path of the current module file

example

console.log(__filename);
// Prints: /Users/mjr/example.js
console.log(__dirname);
// Prints: /Users/mjr
  • It doesn’t solve my problem, I wanted some way to use the root folder of my project.

  • already tried var appRoot = require('app-root-path'); var myModule = require(appRoot + '/lib/my-module.js');

  • I managed to find a way here, thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.