inxilpro / node-app-root-path

Determine the root path to your project
MIT License
605 stars 29 forks source link

Package throws an error when imported in an ES module installed globally on Ubuntu #52

Closed tbroadley closed 2 years ago

tbroadley commented 2 years ago

The error is:

/usr/local/lib/node_modules/app-root-path-ubuntu-repro/node_modules/app-root-path/lib/resolve.js:111
        appRootPath = path.dirname(requireFunction.main.filename);
                                                        ^

TypeError: Cannot read properties of undefined (reading 'filename')
    at resolve (/usr/local/lib/node_modules/spellchecker-cli/node_modules/app-root-path/lib/resolve.js:111:51)
    at module.exports (/usr/local/lib/node_modules/spellchecker-cli/node_modules/app-root-path/lib/app-root-path.js:6:20)
    at Object.<anonymous> (/usr/local/lib/node_modules/spellchecker-cli/node_modules/app-root-path/index.js:4:18)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:170:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)

I've created a repo that you can use to reproduce this error: https://github.com/tbroadley/app-root-path-ubuntu-repro

I think the issue is that app-root-path assumes that requireFunction.main is always defined. However, according to the Node.js documentation:

When the entry point is not a CommonJS module, require.main is undefined, and the main module is out of reach.

Pouja commented 2 years ago

We currently face the same issue