【问题标题】:react-native-dotenv Unable to resolve module @envreact-native-dotenv 无法解析模块@env
【发布时间】:2022-07-05 03:03:14
【问题描述】:

我正在为TypeScript 设置这个库,比如here

我的env:

API_KEY=someKey..

我正在设置type/env.d.ts:

declare module '@env' {
    export const API_KEY: string;
}

我的babel.config.ts:

   module.exports = function (api) {
    api.cache(true);
    return {
        presets: ["babel-preset-expo"],
        plugins: [
            [
                "module:react-native-dotenv",
                {
                    moduleName: "@env",
                    path: ".env",
                },
            ],
        ],
    };
};

我的config.ts:

import API_KEY from '@env'

export default {API_KEY};

package.json:

"dependencies": {
    "@types/react-native-dotenv": "^0.2.0",
    "expo": "~42.0.1",
    "expo-status-bar": "~1.0.4",
    "foo": "^0.0.7",
    "moment": "^2.29.1",
    "react": "16.13.1",
    "react-dom": "16.13.1",
    "react-native": "https://github.com/expo/react-native/archive/sdk-42.0.0.tar.gz",
    "react-native-config": "^1.4.5",
    "react-native-dotenv": "^3.1.1",
    "react-native-web": "~0.13.12",
    "styled-components": "^5.3.0"
  },

以及我使用 API_KEYweather.ts 的这个文件:

Import axios from "axios";
import config from "../../config";

class WeatherService {
  FetchCoordinatesHandler(city) {
    return axios.get(`weather`, {
      params: {
        q: city,
        units: "metric",
        appid: config.API_KEY
      },
    });
  }

  FetchWeatherByCoordinatesHandler({lon, lat}) {

    return axios.get(`onecall`, {
      params: {
        lat: lat,
        lon: lon,
        units: "metric",
        appid: config.API_KEY
      },
    });
  }
}

const weatherServiceInstance = new WeatherService();
export default weatherServiceInstance;

我在控制台中得到了这个:

    Android Bundling failed 8987ms
    Unable to resolve module @env from C:\IT\ReactNative\weather-app\weather-app\config.js: @env could not be found within the project or in these directories:
      node_modules
      ..\node_modules
    > 1 | import API_KEY from '@env'
    |                      ^
  2 |
  3 | export default {API_KEY};

请帮助 :( 我不知道该怎么做,我已经查看了整个互联网。也许我的依赖项没有最新版本

ATTENTION 当我将我的模块从 @env 更改为 'react-native-dotenv' 时,我仍然遇到错误,但还有其他一些错误:

Android Bundling failed 5334ms

    Unable to resolve module fs from C:\IT\ReactNative\weather-app\weather-app\node_modules\react-native-dotenv\index.js: fs could not be found within the project or in t
    hese directories:
      node_modules
      ..\node_modules
    > 1 | const {readFileSync} = require('fs')
        |                                 ^
      2 | const dotenv = require('dotenv')
      3 |
      4 | function parseDotenvFile(path, verbose = false) {

希望有人帮助我,谢谢:)

【问题讨论】:

    标签: typescript react-native environment-variables dotenv


    【解决方案1】:

    fs 问题是关于移动包中不存在该模块,我遇到了同样的问题,我查看了有关该项目的几页,但我没有找到答案,也许这可能会有所帮助,我正在重新配置我的环境配置,

    依赖关系:

    yarn add dotenv react-native-dotenv expo-constants
    

    在根项目中创建 .env 文件

    API_URL=https://api.example.org
    API_TOKEN=abc123
    

    忽略 .gitignore 中的 .env 文件

    在您的根项目中创建或更新文件 app.config.ts,用于导出环境变量和 ts 输入接口(如 https://docs.expo.dev/guides/environment-variables/

    import 'dotenv/config';
    
    export interface AppConfig {
      API_URL: string,
      API_TOKEN: string,
    }
    
    export default {
      name: 'CoolApp',
      version: '1.0.0',
      extra: {
        API_URL: process.env.API_URL,
        API_TOKEN: process.env.API_TOKEN,
      },
    };
    

    创建一个将所有环境变量导出到其他文件的 config.ts 文件

        import Constants from 'expo-constants';
        import { AppConfig } from '../../app.config';
        
        const { API_TOKEN, API_URL } = Constants.manifest?.extra as AppConfig;
    

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 2017-10-11
      • 2016-03-23
      • 1970-01-01
      • 2018-11-18
      • 1970-01-01
      • 2020-04-15
      • 2020-03-02
      • 2016-12-27
      相关资源
      最近更新 更多