【发布时间】: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