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?
The Problem
When creating a bunch of Javascript files, here’s what you can do to refer other files -
|
|
This fetches awesome.js
from the plugins
folder in the parent.
You can do a more robust solution with -
|
|
__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.
The Solution
Use a reference to the main
file to get path to the actual project folder.
|
|
The above line will print out the project folder rather than the package root folder within node_modules
.
To refer plugins
in project folder, you could do -
|
|
See require_main
module for more information.