DEV Community


Posted on • Updated on

Simple Text-Based Adventure Game in Node.js

In this tutorial, we'll walk through creating a simple text-based adventure game in Node.js using the inquirer library for user prompts.

Basic knowledge of JavaScript and Node.js.
Node.js installed on your computer.

Setting Up
First, create a new directory called simple-game:

mkdir simple-game
cd simple-game

Enter fullscreen mode Exit fullscreen mode

Initialize a new npm project:

npm init -y

Enter fullscreen mode Exit fullscreen mode

Install the inquirer library:

npm i inquirer

Enter fullscreen mode Exit fullscreen mode

To allow the use of ES6 import/export syntax, open package.json and add the following line:

"type": "module",

Enter fullscreen mode Exit fullscreen mode

Create a new file named game.js.

// Import the 'inquirer' library to handle user input
import inquirer from 'inquirer';

// Define an array of different game scenarios
const scenarios = [
    // Scenario 1: Introduction
        name: 'intro',
        message: 'You wake up in a mysterious room. What do you do?',
        choices: [
            { name: 'Look around', nextScenario: 'lookAround' },
            { name: 'Open the door', nextScenario: 'openDoor' }
    // Scenario 2: Looking around
        name: 'lookAround',
        message: 'You find an old key on a table and a window that is slightly ajar. What do you do?',
        choices: [
            { name: 'Take the key', nextScenario: 'takeKey' },
            { name: 'Open the window', nextScenario: 'openWindow' }
    // Scenario 3: Opening the door
        name: 'openDoor',
        message: 'The door creaks open, revealing a dark hallway.',
        choices: [
            { name: 'Enter the hallway', nextScenario: 'darkHallway' },
            { name: 'Stay in the room', nextScenario: 'stayInRoom' }
    // ... (more scenarios)
    // Scenario 7: Conclusion
        name: 'ending',
        message: 'Congratulations! You have completed the adventure.',
        choices: []

// Function to present a scenario and get player choice
const presentScenario = async (scenario) => {
    const answers = await inquirer.prompt([
            type: 'list',
            name: 'choice',
            message: scenario.message,
            choices: =>,

    return answers.choice;

// Function to start the game
const startGame = async () => {
    // Start with the 'intro' scenario
    let currentScenario = scenarios.find(scenario => === 'intro');

    // Continue looping through scenarios as long as there's a current scenario
    while (currentScenario) {
        // Present the current scenario to the player and get their choice
        const playerChoice = await presentScenario(currentScenario);

        // Find the next scenario based on the player's choice and update the current scenario
        currentScenario = scenarios.find(scenario => === currentScenario.choices.find(choice => === playerChoice).nextScenario);

    // Print a thank-you message when the game ends
    console.log('Thanks for playing! Goodbye.');

// Start the game by calling the startGame function
Enter fullscreen mode Exit fullscreen mode

This code defines various game scenarios and presents them to the player in sequence.
Each scenario provides the player with multiple choices. When a choice is made, the game moves to the next scenario.

Playing the Game
To play the game, run the following command in your terminal:

node game.js

Enter fullscreen mode Exit fullscreen mode

With just a few lines of code, you've created a simple, engaging text-based adventure game in Node.js! This is just the foundation – you can expand on this by adding more scenarios, integrating more libraries, or even creating a graphical interface.

There you go! A simple tutorial on how to create a text-based adventure game in Node.js. You can further extend this by adding images, sounds, or even making it have a health system and more complex with additional logic and scenarios.

Top comments (0)