Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font.loadAsync() can't resolve module for custom fonts (React Native & Expo)

My app uses CRNA and Expo, and my issue is that the Font.loadAsync() asynchronous function can't locate a .otf font file in the assets/fonts/ folder in my project directory. I am absolutely sure that the directory and file names are correct. I receive this error.

link to image of my error screen

Here is my code:

import React, { Component } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Font, AppLoading } from 'expo'
import Root from './js/Root';

export default class App extends Component {

  constructor(props) {
    super(props)
    this.state = {
      fontLoaded: false
    }
  }

  async componentDidMount() {
    await Font.loadAsync({
      "Light": require('./assets/fonts/SemplicitaPro-Light.otf')
    })
    this.setState({ fontLoaded: true })
  }

  render() {
    if (!this.state.fontLoaded) {
      return <AppLoading />
    }
    return <Root />;
  }
}

Here is my package.json:

    {
  "name": "Zumer",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "flow-bin": "^0.40.0",
    "jest-expo": "^0.4.0",
    "react-native-scripts": "0.0.28",
    "react-test-renderer": "16.0.0-alpha.6"
  },
  "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
  "scripts": {
    "start": "react-native-scripts start",
    "eject": "react-native-scripts eject",
    "android": "react-native-scripts android",
    "ios": "react-native-scripts ios",
    "test": "node node_modules/jest/bin/jest.js --watch",
    "flow": "flow"
  },
  "packagerOpts": {
    "assetExts": ["ttf", "otf"]
  },
  "jest": {
    "preset": "jest-expo",
    "tranformIgnorePatterns": [
      "node_modules/(?!(jest-)?react-native|react-navigation)"
    ]
  },
  "dependencies": {
    "@expo/vector-icons": "^4.0.0",
    "expo": "^16.0.0",
    "native-base": "^2.1.2",
    "react": "16.0.0-alpha.6",
    "react-native": "^0.43.4",
    "react-native-elements": "^0.11.1",
    "react-navigation": "^1.0.0-beta.8",
    "react-redux": "^5.0.4",
    "redux": "^3.6.0",
    "redux-observable": "^0.14.1",
    "redux-persist": "^4.6.0",
    "rxjs": "^5.3.0"
  }
}

Could it be that the .otf file format isn't supported by Expo?

like image 371
Adrian DeRose Avatar asked Apr 29 '17 12:04

Adrian DeRose


2 Answers

I had a similar issue and I was able to resolve it by removing the quotes around the key that requires the font source.

Before:

await Font.loadAsync({
  "Light": require('./assets/fonts/SemplicitaPro-Light.otf')
})

After

await Font.loadAsync({
  Light: require('./assets/fonts/SemplicitaPro-Light.otf')
})

And using it in the stylesheet like so

const styles = StyleSheet.create({
   text: {
      fontFamily: 'Light'
   }
});

Everything worked fine after that.

like image 141
Zakher Masri Avatar answered Nov 16 '22 12:11

Zakher Masri


I fixed this issue by converting the otf files to ttf

like image 28
Lucas Blancas Avatar answered Nov 16 '22 12:11

Lucas Blancas