-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix crash when using from cli #65
Conversation
require.main is undefined when running from cli - don't use unless it exists
Hi @dgobaud, thanks for contributing 👋 Could you please add a bit of context to this PR ? Specifically, I'm looking for repro steps for the bug this PR will fix. I'm not sure I understand what the bug is. Also, before accepting a PR we usually want a few test cases specifying the behavior. Cheers ! |
yes try this
|
I see. What would the use-case be for trying to run module-alias from the cli ? |
I have a Firebase typescript project that uses it. Typescript compiles it to JS in lib folder. I have a utils folder. To make it work when running the compiled JS I do moduleAlias = require("module-alias");
// because of https://github.com/ilearnio/module-alias/pull/65
try { moduleAlias.addPath(path.resolve("")); } catch (e) {};
utils = require("./utils"); |
Mmmh I tried a basic example myself and it worked. I'd like to better understand the problem before fixing it with those if-checks. I probably missed a few details. Could you maybe share a sample codebase showcasing the problem ? In the form of a small github repo ? Anyway, thanks a lot for contributing and helping use improve module-alias 🙇♂️ |
it works BUT you can see the exception above itll crash your code if you dont catch it |
I can't reproduce it locally, see this repo: https://github.com/Kehrlann/module-alias-typescript . Feel free to send you own minimal repo so that I can take a look at it. |
but you don't need type script just do the following node -i and you get below error TypeError: Cannot read property 'paths' of undefined |
Ok I finally got some time to look into it. Summary of the findings:
Then the question is, is it worth it to guard against
I don't think it's worth it, unless there are other more compelling use-cases. |
Ok so i found the issue it is more complicated. From the REPL if I require a file that itself does Below is example generated JS from my TS project that uses module-alias. When I import it in REPL I must first have below module-alias setup or it will fail.
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const js_lib_1 = require("@passfolio/js-lib");
const consts = __importStar(require("consts"));
exports.emailAddComplianceAddresses = (email) => {
if (['production', 'beta'].includes(js_lib_1.kms.settings.CONFIG_ENV)) {
email.bcc = consts.email.COMPLIANCE_EMAIL;
}
};
//# sourceMappingURL=emailAddComplianceAddresses.js.map
|
Ok I see. Sounds good to me ! Thanks for taking the time to explain. Could you please add test cases so we can merge this ? |
require.main is undefined when running from cli - don't use unless it exists