DEV Community

Jeon
Jeon

Posted on

1

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)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay