komaldir
v1.0.0
Published
A simple utility to get the actual project root directory in ES Modules, even when the function is called from inside `node_modules`.
Readme
komaldir
A simple utility to get the actual project root directory in ES Modules, even when the function is called from inside node_modules.
📦 Installation
npm install komaldir✨ Features
- Works with ES Modules
- Returns the actual project root path
- Trims
node_modulesif the package is used inside one - Replacement for
__dirnamein ES Modules
🚀 Usage
📁 Folder Structure
my-app/
├── public/
│ └── prabhu.html
├── index.js
└── package.json🧠 Basic Usage
import { komaldir } from 'komaldir';
const rootDir = komaldir();
console.log(rootDir); // Outputs: absolute path to your project root🌐 Express Example
import express from 'express';
import { komaldir } from 'komaldir';
const app = express();
const pd = komaldir();
const port = 3000;
app.get("/prabhu", (req, res) => {
res.sendFile(pd + '/public/prabhu.html');
});
app.listen(port, () => {
console.log(`Running on ${port}`);
});Visit in browser:
http://localhost:3000/prabhu🔍 Internals
import { dirname } from 'path';
import { fileURLToPath } from 'url';
export function komaldir(metaUrl = import.meta.url) {
let fullPath = dirname(fileURLToPath(metaUrl));
const nodeModulesIndex = fullPath.indexOf('node_modules');
if (nodeModulesIndex !== -1) {
fullPath = fullPath.substring(0, nodeModulesIndex - 1);
}
return fullPath;
}📜 License
MIT License
© 2025 Prabhu R
