
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Parse module aliases from tsconfig ; Apply / remove them from pathnames ; Generate config for webpack & module-alias.
A NodeJS library to parse, process and convert Typescript aliases from tsconfig.json.
We all agree that module aliases are useful to maintain a clean and readable code.
But it's frequent that, in large projects, you have to define your same aliases in multiple tools in order to ensure everything runs correctly.
Also, some tools doesn't support defining custom node_module paths for these aliases, which can be locking in some complex projects.
I wrote this library to avoid the redondancy between my tsconfig.js, Webpack and module-alias configuration in my projects.
I assumed that the tsconfig.js format is the most universal, and can be reliably converted to aliases list for other tools like module-alias and the Webpack package.
By parsing your tsconfig, defining custom rules if necessary, and exporting them for your other aliasing tools, it lightens your maintenance and debugging work.
Are you using this module for another purpose ? Don't hesitate to create a PR so we can list it here!
This project helped you ? Let me know, ⭐ Give it a Star :)
npm i --save ts-alias
The constructor loads the Typescript aliases in the memory. It can be loaded by two different ways:
process.cwd()).import Aliases from 'ts-alias';
const aliases = new Aliases();
rootDir option.
This path can be absolute, or relative (from the process working directory).const aliases = new Aliases({
rootDir: './packages/module-containing-a-tsconfig'
});
const aliases = new Aliases({
rootDir: './packages/module-containing-a-tsconfig/tsconfig.json'
});
An Error will be throwed if rootDir doesn't exists.
If for any reason, you already loaded the tsconfig aliases in memory, you can provide them via the aliases option:
const list = [{
alias: '@server',
// A list of destination paths
pathnames: ['./src/server'],
// If exact = true, only "@server" will be matched
// If exact = false, "@server" and "@server/*" will be matched
exact: false
}, {
alias: 'react',
// pathnames can also be module names
pathnames: ['preact'],
exact: true
}]
const aliases = new Aliases({ aliases: list });
As you saw upper, alias destinations can also be package names.
Thanks to the modulesDir option, you can define in which node_modules directory your package should be looked for.
const aliases = new Aliases({
modulesDir: ['./node_modules', '../../global_node_modules']
});
Warning: This feature is experimental. It could lead to resolution problems in some cases.
Note: Only relative paths are supported for now.
Are you facing to a resolution problem ? Do you balieve these is a bug in this lib ?
That's not impossible 🤔
To better understands what ts-alias actually does in your case, you can enable advanced logs with the debug option:
const aliases = new Aliases({
debug: true
});
public isAliased( filename: string ): boolean;
aliases.isAliased("./src/server/services/user");
// Result: true
aliases.isAliased("./src");
// Result: false
public apply( realpath: string, strict?: false ): string;
public apply( realpath: string, strict: true ): string | null;
public apply( realpath: string, strict?: boolean ): string | null;
aliases.apply("./src/server/services/user");
// Result: "@server/services/user"
aliases.apply("react");
// Result: "./node_modules/react"
When the realpath couldn't be replaced with an alias:
public containsAlias( filename: string ): boolean;
aliases.containsAlias("@server/services/user");
// Result: true
aliases.containsAlias("./src/server/services/user");
// Result: false
public realpath( request: string, strict?: false): string;
public realpath( request: string, strict: true): string | null;
public realpath( request: string, strict?: boolean): string | null;
aliases.realpath("@server/services/user");
// Result: "/home/gaetan/projects/myproject/src/server/services/user"
aliases.realpath("./node_modules/react");
// Result: "preact"
const webpackAliases = aliases.forWebpack();
module.export = {
...
resolve: {
alias: webpackAliases
}
...
}
You can pass options in forWebpack():
const webpackAliases = aliases.forWebpack({
// The path where to resolve node modules
modulesPath: string,
// Set to true if you want forWebpack to output the package name (ex: `ts-alias/src/index.ts`) instead of the path to node_modules (ex: `./node_modules/ts-alias/src/index.ts`)
shortenPaths: boolean,
// When set to true, it will return a { aliases, externals } object with the aliases for webpack,
// and a nodeExternals function for webpack
nodeExternals: boolean
});
import moduleAlias from 'module-alias';
moduleAlias.addAliases( aliases.forModuleAlias() );
This project helped you ? Let me know, ⭐ Give it a Star :)
public forWebpack<TNodeExternals extends boolean>({
modulesPath, shortenPaths, nodeExternals
}: TOutputOptions<TNodeExternals>): TWebpackOutput<TNodeExternals>
Added a shortenPaths option for forWebpack()
Set to true if you want forWebpack to output the package name (ex: ts-alias/src/index.ts) instead of the path to node_modules (ex: ./node_modules/ts-alias/src/index.ts)
Store aliases in array instead of indexing by alias. It allows you to create an exact & prefix version for the same alias. By example:
{
// Exact version: only match "@server"
"@server": ["./server"],
// Prefix version: match any import that starts by "@server/"
"@server/*": ["./server/*"],
}
FAQs
Parse module aliases from tsconfig ; Apply / remove them from pathnames ; Generate config for webpack & module-alias.
We found that ts-alias demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.