Search code examples
node.jsexpressfs

Node fs.readdirsync read directory in npm package


i tried to use fs.readdirSync and it work perfectly, but when i publish to npm and install this package to my new project, it not read my package directory instead it will read from my new project directory.

My goal is to get list file names inside package directory not new project directory

const dirents = fs.readdirSync("./", { withFileTypes: true });
console.log(dirents)

Solution

  • You need to use __dirname because it is an environment variable that tells you the absolute path of the directory containing the currently executing file.

    Instead of using ./ it will represent your current working directory