Closed flolu closed 4 years ago
I got it working with this implementation
const moduleAlias = require('module-alias');
const addPackageAlias = (name: string) =>
moduleAlias.addAlias(`@module-name/${name}`, `${__dirname}../../../modules/${name}`);
const fs = require('fs');
const paths: string[] = fs.readdirSync('../../../modules');
paths.forEach(addPackageAlias);
But nevertheless, a native implementation would be appreciated :)
Hey @flolu, "wildcarding" should work by default. Suppose the following package.json:
"_moduleAliases": {
"@module-name": "base/for/your/modules"
}
You should then be able to use it this way:
require("@module-name");
require("@module-name/moduleA");
require("@module-name/some/deep/module");
@Kehrlann Sorry, it didn't work for me :disappointed: Maybe I did something wrong, not sure. You can try it by cloning this project if you want to: https://github.com/flolu/cents-ideas
Oooh. This is a more complicated project, with ts-node
... Can't get it to work here, running npx ts-node services/users
and such. I might take a look tomorrow but can't promise anything.
Simple project: https://github.com/kehrlann/module-alias-library. It shows multiple things, but I just added an alias "wildcard" showing exactly what you mention. Run it with
node index.js`
Are there any updates on this? Still doesn't seem to work with ts-node
Alias HQ is built around ts/jsconfig
with plugins for major frameworks.
@davestewart Thanks for the tip! I've already got it working by using tsconfig-paths
Instead of running
ts-node file
I just run
ts-node -r tsconfig-paths/register file
It will automatically pick up the paths from tsconfig.json
@ilearnio @Kehrlann Should this issue be closed now?
Yep, for a Typescript project, that makes a ton of sense ! Thanks for trying that out.
Feature request
Instead of writing
it would be handy to just write:
@ilearnio Or is it possible to achieve this with a custom handler function?