How to import a non-TypeScript module by relative path?

Viewed 448

I have the following file structure:

.
├── a
│   ├── package.json
│   ├── src
│   │   └── main.ts
│   └── tsconfig.json
└── b
    ├── package.json
    └── index.js

a is a TypeScript module, b is a plain JavaScript module.

a/src/main.ts contains the following code:

import {helloB} from "../../b";
console.log("Hello from A!");
helloB();

b/index.js contains the following code:

module.exports.helloB = function() {
    console.log("Hello from B!");
};

I would like to import b from a using a relative import. I've tried importing ../../b as specified above but got the following error:

src/main.ts:1:22 - error TS6059: File '/home/ivanq/Documents/tstest/b/index.js' is not under 'rootDir' '/home/ivanq/Documents/tstest/a/src'. 'rootDir' is expected to contain all source files.

1 import {helloB} from "../../b";

I could add b to rootDir but I do not want TypeScript to compile that module and output it to build directory. I want it to do require("../../b") in runtime, like it does with files inside node_modules. How do I configure TypeScript to do that?

Here's my tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "outDir": "build",
    "rootDir": "src",
  },
  "include": ["src"]
}
0 Answers
Related