module-alias
module-alias copied to clipboard
A way for VSCode to detect the location of the aliased directory
Using the normal require function gives the developers benefit of hovering over the function then VSCode will be able to show the necessary arguments of the function. Moreover, you can click alt
to go directly to the hovered function. Is there a way to do this using this library?
An image using the normal require function
Requiring a module using module-alias
You need to create a file inside of the root project, called jsconfig.json
with the following properties:
{
"compilerOptions": {
"baseUrl": "./",
"module": "commonjs",
"paths": {
"@shared/*": ["./shared/*"],
"@modules/*": ["./modules/*"],
"@config/*": ["./config/*"],
"@root/*": ["./*"]
}
},
"exclude": ["node_modules"]
}
Did you already try this one?
You need create a file inside of the root project, called jsconfig.json with the nexts properties:
{ "compilerOptions": { "baseUrl": "./", "module": "commonjs", "paths": { "@shared/*": ["./shared/*"], "@modules/*": ["./modules/*"], "@config/*": ["./config/*"], "@root/*": ["./*"] } }, "exclude": ["node_modules"] }
Did you already try this one?
Wow! you got it! Thank you
You need to create a file inside of the root project, called
jsconfig.json
with the following properties:{ "compilerOptions": { "baseUrl": "./", "module": "commonjs", "paths": { "@shared/*": ["./shared/*"], "@modules/*": ["./modules/*"], "@config/*": ["./config/*"], "@root/*": ["./*"] } }, "exclude": ["node_modules"] }
Did you already try this one?
Thank you, it was a life saver 🙏
Hello to everyone,
If I may, using this config in jsconfig.json
may disrupt the default behavior of the eslint
VSCode extension (it happened to me last year)
This is my module-alias
config in package.json
:
"_moduleAliases": {
"@": "build/src"
}
To fix intellisense in VSCode
, please follow these steps :
- In VSCode extensions, install the extension called
Path Intellisense
- Then at the root of your project, create a
.vscode
folder, and inside this folder, create asettings.json
file and paste your config.
My vscode config file looks like this:
{
"path-intellisense.mappings": {
"@": "${workspaceRoot}/src"
},
}
You can refer to this repository for a complete example
Best reguards
You need to create a file inside of the root project, called
jsconfig.json
with the following properties:{ "compilerOptions": { "baseUrl": "./", "module": "commonjs", "paths": { "@shared/*": ["./shared/*"], "@modules/*": ["./modules/*"], "@config/*": ["./config/*"], "@root/*": ["./*"] } }, "exclude": ["node_modules"] }
Did you already try this one?
It did not work for me in Nodejs(18.13.0) Project. Could you please look at this?
node:internal/modules/cjs/loader:1042
throw err;
^
Error: Cannot find module '@routersV1/users/usersRouter'node:internal/modules/cjs/loader:1042
throw err;
^
Error: Cannot find module '@routersV1/users/usersRouter'
package.json
"_moduleAliases": {
"@root": "./",
"@utils/*": "./src/utils/*",
"@controllersV1/*": "./src/controllers/v1/*",
"@modelsV1/*": "./src/models/v1/*",
"@routersV1/*": "./src/routers/v1/*",
"@servicesV1/*": "./src/services/v1/*",
"@middlewares/*": "./src/middlewares/*"
}
jsconfig.json
{
"compilerOptions": {
"baseUrl": "./",
"target": "es6",
"module": "commonjs",
"allowJs": true,
"checkJs": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"outDir": "./dist",
"strict": false,
"paths": {
"@/*": ["src/*"],
"@utils/*": ["src/utils/*"],
"@controllersV1/*": ["src/controllers/v1/*"],
"@modelsV1/*": ["src/models/v1/*"],
"@routersV1/*": ["src/routers/v1/*"],
"@servicesV1/*": ["src/services/v1/*"],
"@middlewares/*": ["src/middlewares/*"]
},
"lib": ["es6", "dom"]
},
"exclude": ["node_modules", "**/node_modules/*", "dist"]
}
ensure that require('module-alias/register') is run prior to calling any alias directory otherwise you will get error { "name": "your-package", "version": "1.0.0", "scripts": { "start": "node -r module-alias/register src/app.js" }, "dependencies": { "module-alias": "^2.2.2" }, "_moduleAliases": { "@root": ".", "@src": "./src" } }