React Native 无法解析模块:Cannot resolve

cig3rfwq  于 2023-04-12  发布在  React
关注(0)|答案(2)|浏览(223)

我正在为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_KEY weather.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) {

  • 希望大家能帮帮我,谢谢:)*
nzk0hqpo

nzk0hqpo1#

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;
j91ykkif

j91ykkif2#

按照此步骤操作,问题将得到解决。
1.明确的守望者手表:守望者
1.删除节点模块(_M):rm -rf node_modules并运行yarn install
1.重置Metro的缓存:yarn start --reset-cache
1.删除该高速缓存:rm -rf /tmp/metro-*

相关问题