You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用React Native Config for Android时缺失.env文件,请求协助排查

How to Fix "JS server already running" and "Missing .env file" in React Native

Hey there, let's tackle your two React Native issues one by one—they're both pretty common and straightforward to resolve!

Issue 1: "JS server already running"

This error happens because the Metro bundler (React Native's JS server) is still running in the background, even if you thought you closed it. Here's how to fix it:

  • Quick first step: If you have the Metro terminal window open, press Ctrl + C to terminate the process, then restart your app with npx react-native run-android.
  • If the server lingers: Kill the Metro process manually:
    • On macOS/Linux: Run these commands in your terminal:
      lsof -i :8081  # Find the process ID (PID) using port 8081
      kill -9 <PID>  # Replace <PID> with the number from the previous command
      
    • On Windows: Open Task Manager, locate all node.exe processes linked to React Native/Metro, right-click and select "End Task".
  • Reset cache and restart: You can also clear cached data and start fresh with this command:
    npx react-native start --reset-cache
    

Issue 2: "Missing .env file"

Your build is trying to load environment variables from a .env file that doesn't exist in your project root. Here's what to do:

  1. Create the .env file: Head to your project's root directory (not the android folder) and make a new file named .env.
  2. Add your environment variables: Fill it with the variables your app needs, for example:
    API_BASE_URL=https://your-api-endpoint.com
    APP_DISPLAY_NAME=MyAwesomeApp
    
  3. Verify your env setup: If you're using a library like react-native-dotenv, double-check your babel.config.js to ensure it's configured to read the .env file:
    module.exports = {
      presets: ['module:metro-react-native-babel-preset'],
      plugins: [
        ['module:react-native-dotenv', {
          moduleName: '@env',
          path: '.env',
          allowUndefined: false
        }]
      ]
    };
    
  4. Rebuild the app: Go back to the android folder and run a clean build to make sure the new .env file is picked up:
    cd android && ./gradlew clean installDebug
    

After following these steps, both issues should be sorted out. Let me know if you hit any unexpected snags!

内容的提问来源于stack exchange,提问作者user1106888

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:48:49