If you’re like me, you love the grind in Minecraft… until you don’t.
Planting, harvesting, replanting—it’s satisfying at first, but once you’ve done it 40 times, it gets old. That’s when I thought:
“Wait a second… I write bots for the web. Why not for Minecraft?”
So I did. With a bit of Node.js, a fantastic library called mineflayer
, and some trial and error, I built a smart auto-farming bot that logs into my server, walks over to my farm, harvests crops, replants them, and chills until the next cycle.
What You’ll Need:
- Before diving in, here's what you'll need:
- A Minecraft Java Edition account
- A local or hosted Minecraft server (supporting bots)
- The mineflayer library
- A basic understanding of JavaScript and game loops
Step 1: Set Up Your Project
Start fresh:
mkdir mc-auto-farmer cd mc-auto-farmer npm init -y npm install mineflayer vec3
Then create your entry file:
touch farmer.js
Step 2: Connect to the Minecraft Server
Let’s start simple — login to Minecraft APK and spawn into the world.
// farmer.js const mineflayer = require('mineflayer'); const bot = mineflayer.createBot({ host: 'localhost', // your server IP port: 25565, // default Minecraft port username: 'FarmBot3000' // no auth needed for offline servers }); bot.on('spawn', () => { console.log('🧑🌾 Bot has spawned and is ready to farm!'); });
Run with:
node farmer.js
Boom. You're in.
Step 3: Walk to the Farm
Now we need to guide the bot to the crop area. We’ll manually define the coordinates.
const { Vec3 } = require('vec3'); const farmLocation = new Vec3(100, 64, 100); // change to your farm's coords bot.on('spawn', () => { bot.chat('Heading to the farm!'); bot.pathfinder.setGoal(new GoalBlock(farmLocation.x, farmLocation.y, farmLocation.z)); });
But wait — we forgot something. pathfinder!
Install and load it:
npm install mineflayer-pathfinder
And require it properly:
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder'); const { GoalBlock } = goals; bot.loadPlugin(pathfinder); bot.on('spawn', () => { const defaultMove = new Movements(bot); bot.pathfinder.setMovements(defaultMove); bot.pathfinder.setGoal(new GoalBlock(farmLocation.x, farmLocation.y, farmLocation.z)); });
Now your bot will navigate to the farm like a good little NPC.
Step 4: Harvest + Replant Crops
This is the fun part.
We'll detect when the bot arrives, then scan for crops (like wheat) that are fully grown.
bot.on('goal_reached', async () => { bot.chat('🌾 Arrived at the farm. Looking for ripe crops...'); const crops = bot.findBlocks({ matching: block => block.name === 'wheat' && block.metadata === 7, maxDistance: 6, count: 20, }); for (const pos of crops) { const block = bot.blockAt(pos); if (block) { await bot.dig(block); // harvest await bot.placeBlock(bot.blockAt(pos.offset(0, -1, 0)), new Vec3(0, 1, 0)); // replant } } bot.chat('✅ Farming complete. Waiting for next cycle...'); });
We’re using metadata === 7 because that means the wheat is fully grown.
Step 5: Loop It with a Timer
Let’s run this farming cycle every 5 minutes.
setInterval(() => { bot.chat('⏳ Checking crops again...'); bot.pathfinder.setGoal(new GoalBlock(farmLocation.x, farmLocation.y, farmLocation.z)); }, 5 * 60 * 1000);
Optional: Add random intervals to make it less bot-like.
Final Thoughts
This project was a blast to build. Not only did I automate one of Minecraft’s most repetitive tasks, but I also learned how accessible Minecraft automation is with JavaScript.
Where to Take This Next
- Add chest storage logic (deposit harvested wheat)
- Support multiple crop types (carrots, potatoes, etc.)
- Build a Discord bot to report farming stats
- Use mineflayer’s inventory API to manage seeds/tools
- Detect hostile mobs and run away
Bonus Ideas
- Create a farming network of multiple bots
- Connect your farm bot with a weather plugin (don’t farm in the rain)
- Auto-craft bread from harvested wheat
- Visualize farm productivity with a dashboard
Top comments (0)