I'm using expo-cli.
I import a module from the folder 'src' (i created this folder), if open in web this work perfectly, but if open in android not found this.
I try use the absolute path, the Babel Resolver module and nothing work.
Project:
-assets
-node_modules
-src
-components
-screens
-HomeScreen.js
-services
-Webservices.js
-styles
-utils
-App.js
...
Screen file:
...
import WebServices from '../services/WebServices';
export default class HomeScreen extends React.Component {
...
File to import:
const axios = require('axios');
var url = 'https://randomuser.me/api/';
var WebServices = {
getUser: function(){
return axios.get(url);
}
}
export { WebServices as default };
Error:
Android Bundling failed 5347ms
Unable to resolve module ../services/WebServices.js from C:\Users\jalvarez\Proyectos\AppPruebas\src\screens\HomeScreen.js:
None of these files exist:
* src\services\WebServices.js(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
* src\services\WebServices.js\index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
5 | import { createNativeStackNavigator } from '@react-navigation/native-stack';
6 |
> 7 | import WebServices from '../services/WebServices.js';
| ^
8 |
9 | export default class HomeScreen extends React.Component {
Installing Typescript and babel-plugin-root-import modules and with this config in babel.config.js and tsconfig.json work for me in android and web.
balbel.config.js:
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: [
[
'babel-plugin-root-import',
{
root: __dirname,
rootPathPrefix: '~',
rootPathSuffix: './src',
},
]
]
};
};
In tsconfig.json add:
"rootDir": "./"
For import:
import WebServices from '~/services/webservices';