Is there a way to make TypeScript imports function properly even if the node_modules directory is not directly in the tree?
How can I prevent TypeScript from throwing errors when importing something like rxjs
from external/node_modules
.
For Example:
Directory Structure:
|-- external
| `-- package.json
|-- index.ts
`-- tsconfig.json
Contents of tsconfig.json:
{
"compilerOptions": {
"module": "amd",
"target": "es5",
"sourceMap": true,
"removeComments": true,
"allowJs": false,
"pretty": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"lib": [ "es2015", "dom" ]
}
}
Contents of external/package.json:
{
"name": "external",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"rxjs": "^5.0.3"
}
}
Contents of index.ts:
import { Observable } from 'rxjs/Rx';
Observable
Navigate to the external directory:
Run yarn install command:
yarn add v0.18.1
[1/4] 🔍 Resolving packages...
[2/4] 🚚 Fetching packages...
[3/4] 🔗 Linking dependencies...
[4/4] 📃 Building fresh packages...
success Saved lockfile.
success Saved 2 new dependencies.
├─ [email protected]
└─ [email protected]
✨ Done in 1.39s.
Check TypeScript version:
Version 2.1.4
Compile TypeScript:
1 import { Observable } from 'rxjs/Rx';
~~~~~~~~~
../index.ts(1,28): error TS2307: Cannot find module 'rxjs/Rx'.