使用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 + Cto terminate the process, then restart your app withnpx 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.exeprocesses linked to React Native/Metro, right-click and select "End Task".
- On macOS/Linux: Run these commands in your terminal:
- 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:
- Create the .env file: Head to your project's root directory (not the
androidfolder) and make a new file named.env. - 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 - Verify your env setup: If you're using a library like
react-native-dotenv, double-check yourbabel.config.jsto ensure it's configured to read the.envfile:module.exports = { presets: ['module:metro-react-native-babel-preset'], plugins: [ ['module:react-native-dotenv', { moduleName: '@env', path: '.env', allowUndefined: false }] ] }; - Rebuild the app: Go back to the
androidfolder and run a clean build to make sure the new.envfile 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
相关产品推荐
相关产品推荐

