You can refer to other files/folders using a simple
require or using basic node libraries. But, how can you do that with npm packages?
const awe = require("../plugins/awesome.js");
awesome.js from the
plugins folder in the parent.
You can do a more robust solution with -
const awe = require(path.join(__dirname, "../", "plugins"));
__dirname fetches the current directory, and you just join that with the path to reach to the file or folder.
But, what if you are packaging your app?
__dirname refers to the packaged app folder, and is buried under
node_modules as far as the consumer of the package is concerned.
Use a reference to the
main file to get path to the actual project folder.
const path = require("path"); console.log(path.dirname(require.main.filename));
The above line will print out the project folder rather than the package root folder within
plugins in project folder, you could do -
const awe = require(path.join(path.dirname(require.main.filename), "plugins"));
require_main module for more information.