Typescript import from root path not working

Viewed 25

In my tsconfig.json I have set outDir to lib, and baseURL to ./src. (This is in my typescript firebase functions project)

{
  "compilerOptions": {
    "outDir": "lib",
    "baseUrl": "./src",
    "paths": {
      "~/*": ["./*"]
    },
    ...
  }
  ...
}

Folder Struccture

    src
    ├── tsconfig.json
    ├── lib
    │   ├── test.js
    │
    │── test.ts
    │── data
    │   ├── names.json

Import Code (test.ts)

    import names from "~/data/names.json";

But when it compiles, it uses the folder lib as a root path in imports, hence says Module not found

The error

    !!  functions: Failed to load function definition from source: FirebaseError: Failed to load function definition from source: Failed to generate manifest from function source: 
    Error: Cannot find module '~/data/names.json'
    Require stack:
    - C:\Proj\src\lib\test.js

How can the referencing be fixed in tsconfig or other way?

1 Answers

Move tsconfig.json out of the src folder, and change the outDir field with:

"outDir": "src/lib",
Related