Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module '@env' or its corresponding type declarations

Why I get this TypeScript error message and how can I fix it ?

Cannot find module '@env' or its corresponding type declarations

This occurs when I use this:

import { FB_ID } from '@env';

Tt should be named not @env, it should named 'react-native-dotenv' but in my config I named it @env so I can use it, and I can retrieve the FB_ID value. It works but I get a TypeScript error.

This is where I set the moduleName:

      ['module:react-native-dotenv', {
        "envName": "APP_ENV",
        "moduleName": "@env",
        "path": ".env",
        "blocklist": null,
        "allowlist": null,
        "safe": false,
        "allowUndefined": true,
        "verbose": false
      }]

So how can I fix it ?

tsconfig:

{
  "extends": "expo/tsconfig.base",
  "compilerOptions": {
    "strict": true,
    "baseUrl": ".", // this must be specified if "paths" is specified.
    "paths": {
      "@env": ["node_modules/react-native-dotenv"] // this mapping is relative to "baseUrl"
    }
  }
}
like image 703
universe11 Avatar asked Nov 19 '25 03:11

universe11


2 Answers

  • Create a types folder in your source folder.
  • Create a file named env.d.ts in the types folder.
  • Define your env variables as strings.
    declare module '@env' {
      export const FB_ID: string;
    
      // other ones
    }
    
like image 127
GollyJer Avatar answered Nov 21 '25 00:11

GollyJer


You should probably look at the paths property of the compilerOptions of your tsconfig file which looks like something alongs the line of:

{
  "compilerOptions": {
    "baseUrl": ".", // this must be specified if "paths" is specified.
    "paths": {
      "@env": ["node_modules/<path to the module dist>"] // this mapping is relative to "baseUrl"
    }
  }
}

This will tell to typescript that @env point to the module and will stopped throwing errors about it. Otherwise it has no idea about the resolution of it and will only look for node_modules/@env

like image 26
Adrien De Peretti Avatar answered Nov 20 '25 23:11

Adrien De Peretti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!