Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript: set "node_modules" directory path

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?

like image 831
Kshatra Avatar asked Jan 21 '19 13:01

Kshatra


People also ask

How do I change the location of a node module?

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.

Can I move 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)

Where does node look for node_modules?

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.

What is the node_modules directory?

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.


2 Answers

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.

like image 69
David Callanan Avatar answered Oct 14 '22 03:10

David Callanan


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

like image 1
Domiserver Avatar answered Oct 14 '22 03:10

Domiserver