How can i set/configure a resolution path for the whole "node_modules" directory (not separate modules) for Typescript compiler if that directory is not located in the default resolution path?
js documentation and found that it can be changed very easily using a simple "npm config" command. For example: npm config set prefix "E:\node_modules", From the next time onward, every global installation will save the node modules in "E:\node_modules" folder.
Yes you can copy whole node_modules (have done it multiple times) from one project to another and use same package. json and package-lock (Will only save time in dependencies installation/download)
Node will look for your modules in special folders named node_modules . A node_modules folder can be on the same level as the current file, or higher up in the directory chain. Node will walk up the directory chain, looking through each node_modules until it finds the module you tried to load.
The node_modules folder is used to save all downloaded packages from npm in your computer for the JavaScript project that you have. Developers are always recommended to do a fresh install with npm install each time they downloaded a JavaScript project into their computer.
You may change where TypeScript looks for the node_modules
folder as described here: https://www.typescriptlang.org/docs/handbook/module-resolution.html
Setting baseUrl informs the compiler where to find modules. All module imports with non-relative names are assumed to be relative to the baseUrl.
Add the following to your tsconfig.json
file:
"compilerOptions": {
"baseUrl": "<dir_containing_node_modules>"
}
Sadly it looks like you cannot specify a different name for the node_modules
folder - although this is a less likely situation. Please correct me if I'm wrong.
This is the way I solved this issue with Typescript within the root directory:
I set tsconfig.json to:
"compilerOptions": {
"baseUrl": "./",
"typeRoots": ["node_modules/@types"],
}
I hope this helps someone to complement the previous answer.
RON
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With