frontend/ReactNative

react-native env 설정하기 || error: Error: Unable to resolve module @env

머스타드코드 2023. 4. 28. 15:06

 

$ npm install -D react-native-dotenv

 

babel.config.js

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
    plugins: [
      ['module:react-native-dotenv', {
        moduleName: 'react-native-dotenv',      
        path: '.env',
      }],
    ],
};

프로젝트 루트에 .env파일 만들어서

API_KEY=어쩌구

 

src를 쓰고 있다면 src/types/env.d.ts

declare module 'react-native-dotenv' {
    export const API_KEY: string;
}

사용방법

import {API_KEY} from 'react-native-dotenv'

tsconfig.json에

밑에처럼 typesRoots를 설정해주거나

"include": ["src/**/*", "src/types/**/*"] 를 해준다.

 

참고

https://issuehunt.io/r/goatandsheep/react-native-dotenv/issues/52