reactjs - 开玩笑找不到模块@env React Native
问题描述
我目前遇到了一些问题,我在我的反应原生应用程序https://github.com/goatandsheep/react-native-dotenv中使用它来处理.env。
错误 => 无法从“src/api/api.ts”中找到模块“@env”
我目前正在测试我的 redux 传奇以调用 api 端点:
import axios from 'axios';
import {API_URL} from '@env';
export default axios.create({
baseURL: API_URL,
responseType: 'json',
});
API端点
export const sendCheckNumber = async (
phoneNumber: string,
): Promise<AuthenticationTO> => {
const response = await axios.get<AuthenticationTO>(
`SendCheckNumber/${phoneNumber}`,
);
return response.data;
};
我正在使用 ts-jest package.json。我在文档中看到它可以在 ts-jest 中包含 bable,因为我正在使用 bable 将“模块:react-native-dotenv”作为插件导入。我认为这已经解决了我的问题,但不幸的是它仍然失败。也许你们中的一些人有一些建议可能导致这个错误。
谢谢!!!
包.json
"jest": {
"preset": "react-native",
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
"\\.(ts|tsx)$": "ts-jest"
},
"globals": {
"ts-jest": {
"babelConfig": "babel.config.js",
"tsConfig": "tsconfig.jest.json"
}
},
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"modulePaths": [
"<rootDir>/src"
],
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$"
}
解决方案
我解决了这个问题,如下所示。
import {API_URL} from '@env';`
改为添加:
import {API_URL} from 'react-native-dotenv';
并在babel.config.js中添加下一个
module.exports = function (api) {
api.cache(true)
return {
presets: ["babel-preset-expo"],
plugins: [
[
"module:react-native-dotenv",
{
moduleName: "react-native-dotenv",
},
],
],
}
}
这对我来说很有用。
推荐阅读
- php - Laravel - 我的解释不能完美地工作
- android - .NET Core Web API 推送到 Apple/Android 推送通知服务器
- jquery - 使用选择下拉菜单在选项卡之间切换
- react-native - 标题标题不显示
- printing - 媒体打印在 Safari 上显示白页
- c++ - 如何在 QSplashScreen 中禁用鼠标单击
- ruby-on-rails - 没有得到具体的答案
- php - 如果没有包含已删除产品的行,则无法显示包含有关 woocommerce 产品信息的 mysql wpdb 表;也无法对 html 表格进行分页
- threat-model - 如何在 Microsoft 威胁建模工具中表示 Azure Function?
- arrays - 在 Swift 中收集和随机化数据