tbroadley / spellchecker-cli

A command-line tool for spellchecking files.
MIT License
119 stars 16 forks source link

"TypeError: Cannot read properties of undefined (reading 'filename')" when globally installed on Ubuntu #91

Closed xoxys closed 1 year ago

xoxys commented 2 years ago

Hi, spellchecker-cli v5 doesn't work anymore on a library/node:lts-alpine container:

/ # npm install -g spellchecker-cli
/ # spellchecker --help
/usr/local/lib/node_modules/spellchecker-cli/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)
xoxys commented 2 years ago

I have to correct myself, it's not an alpine specific issue it also doesn't work on library/node:lts-slim? Any idea what's wrong?

tbroadley commented 2 years ago

Thank you for the issue!

https://nodejs.org/api/modules.html#accessing-the-main-module

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

So I'm guessing this is related to index.js no longer being a CommonJS module. It's a bit strange that I didn't see this issue when I ran the tool on Windows, though.

In any case, this is potentially an issue with the app-root-path NPM module. I'll try to come up with a minimal repro case for them.

tbroadley commented 2 years ago

OK I can reproduce this. I notice that this bug only occurs when I install Spellchecker CLI globally in the container. If I install it in an NPM package (after running npm init), I don't see the error.

tbroadley commented 2 years ago

Just filed https://github.com/inxilpro/node-app-root-path/issues/52.

xoxys commented 1 year ago

Not sure if the upstream issue will ever get solved after the last commit is 3 years old.

tbroadley commented 1 year ago

Yes, that's true. The app-root-path readme does say "expect long stretches of zero updates". Still, that repo has a number of unaddressed issues and PRs.

I'll probably change spellchecker-cli to not depend on app-root-path anymore.

tbroadley commented 1 year ago

This should be fully fixed in 6.0.1.

xoxys commented 1 year ago

Awesome! Thanks for the fix.