DEV Community

Jeon
Jeon

Posted on

Read Text Asset File in Expo

§ Install expo-asset and expo-file-system:

npx expo install expo-asset expo-file-system
Enter fullscreen mode Exit fullscreen mode

§ Add expo-asset plugin in app.config.js (or in app.json), if it has not been added by the previous step:

   plugins: [
+    "expo-asset"
   ]
Enter fullscreen mode Exit fullscreen mode

§ Install @expo/metro-config:

npm install --save-dev @expo/metro-config 
Enter fullscreen mode Exit fullscreen mode

§ Create metro.config.js:

 const {  getDefaultConfig } = require("expo/metro-config");

 const config = getDefaultConfig(__dirname);
+config.resolver.assetExts.push("txt");

 module.exports = config;
Enter fullscreen mode Exit fullscreen mode

§ Create or put a text file under /assets folder

§ Code:

async function readTextAsset() {
  try {
    const nodeRequire = require("@/assets/filename.txt");
    const asset = Asset.fromModule(nodeRequire);
    await asset.downloadAsync();
    if (asset.localUri) {
      const fileContents = await readAsStringAsync(asset.localUri);
      // Do something with `fileContents`
    }
  } catch (error) {
    console.error(error);
  }
}
Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay