Reputation: 3159
I'm using app-module-path, for resolving require statements without having to explicitly provide the relative path to the module. But vscode cant recognize the module since it's not relative path. I had tried using jsconfig
by following this thread. I created the jsconfig in the root of my project. But the path resolution done by vscode conflicted with app-module-path
, since vscode adds a server/
prefix before all paths. What changes should I make to jsconfig.json
inorder to make it work with app-module-path
?
.
├── jsconfig.json
├── package.json
├── server
│ ├── app.js --------> here I set the app-module-path
│ ├── controllers
│ ├── helpers
│ │ ├── email
│ │ │ ├── fetch.js
│ │ └── urlparser.js
│ └── services
│ └── github
│ └── index.js-> i want import "helpers/email/fetch" from here, not "server/helpers/email/fetch"
└── yarn.lock
With app-module-path
set in app.js
in base of server
, I am able to do the following from server/services/github/index.js
:-
const {emailFetch} = require("helpers/email/fetch"); // Note: I cant use the "server/" prefix before "helpers" since app-module-path will lead to error
const {emailFetch} = require("server/helpers/email/fetch"); // This is how vscode auto-imports
This is how I have registered the app-module-path
in app.js
:-
require("app-module-path").addPath(__dirname);
With the following jsconfig.json
, vscode gives intellisense, signature help, go to definition etc even if the path is helpers/email/fetch
(without server/
prefix). But how can i make vscode auto-import or fix the require
statements, in way such that it doesn't add server
prefix before all import statements? Should I use some vscode-extension for doing this job?
{
"compilerOptions": {
"target": "ES6",
"allowSyntheticDefaultImports": true,
"module": "commonjs",
"baseUrl": ".",
"paths": {
"*": [
"*",
"server/*"
]
}
},
"include": [
"server/**/*.js"
]
}
Upvotes: 2
Views: 1791
Reputation: 3159
Just posting my answer too over here, so that someone else finds it useful. Currently I have made it to work by changing baseUrl
into "server"
and removing "server/*"
from paths
, in jsconfig.json
. With this I get correct auto-imports + change in import paths while I move files around too. Do post your answers if you know to do this is in a better way. Cheers!
Upvotes: 1