javascript detecting file path

51 views Asked by At

i am going to make a npm package that can check the main file if it's exist how can i detect the file path?

main file

index.js

  const exampleCheck = require("exampleCheck")

exampleCheck(__dirname);

node_modules

 const path = require("path");
 const fs = require("fs-extra");

module.exports = async function(file) {
  if (!fs.existsSync(path.join(file, "index.js"))) return console.log("index.js doesn't exist");
}

my question is why the fs.existSync on my package at node_modules always return false but the index.js is exist at the main file

i hope you understand my english haha please help me a lot

my package always says index doesn't exist but it's exist at the main file

finding the file path

1

There are 1 answers

2
emir On
const path = require("path");
const fs = require("fs-extra");

module.exports = async function () {
  // Find the main project directory using the parent module's __dirname
  const mainModule = module.parent;
  if (mainModule) {
    const projectDirectory = path.dirname(mainModule.filename);
    if (fs.existsSync(path.join(projectDirectory, "index.js"))) {
      console.log("index.js exists in the main project directory.");
    } else {
      console.log("index.js doesn't exist in the main project directory.");
    }
  } else {
    console.error("Could not determine the main project directory.");
  }
};