Getting Started with Plop: What Is a Code Generator?

Learn what Plop does, when to use it, and how to install it and run your first file generator.

🎯 Introduction

In this article, you will learn what Plop is, which problems it solves, and how to get started in your project. We will install Plop, configure our first generator, and generate a file automatically.


🧱 What Is Plop?

Everyday development often involves creating files with similar structures, such as:

  • Vue components: UserCard.vue, OrderList.vue, ProductDetail.vue, and so on.
  • API files: getUser.js, createOrder.js, updateProduct.js, and so on.
  • Utility functions: formatDate.js, debounce.js, cloneDeep.js, and so on.

Manually creating files, copying existing ones, and updating class names and filenames each time is tedious and prone to errors.

Plop is a lightweight code generator framework. It takes templates you prepare in advance, asks a few interactive questions, and generates files that follow your conventions.

Think of it as a fill-in-the-blank form paired with a copier. Prepare a standard form (the template), answer a few questions (the variables), and produce a file with the answers filled in.


✨ Why Choose Plop?

Plop offers several advantages:

  • Easy to get started: A small configuration file and a few lines of code are enough to get it running.
  • Template-driven: It uses Handlebars syntax, familiar from frontend templates.
  • Interactive: Command-line prompts collect variables such as filenames and component names.
  • Consistent conventions: Everyone on the team generates the same file structure, reducing differences in style.
  • Lightweight tooling: It runs on Node.js without complex build tools.
Plop is especially useful for generating files that follow a fixed pattern. If a category of files in your project shares a common structure, consider automating their creation with Plop.

🚀 Installing Plop

This course uses Node.js v22.0.0 and Plop 4.0.5.

Create a project and install Plop:

mkdir plop-demo
cd plop-demo
npm init -y
npm install plop@4.0.5 --save-dev

After installation, run it with npx plop:

npx plop
Plop 4.x uses ES Modules (ESM) by default. All examples in this course use plopfile.mjs as the configuration file, so you do not need to change the type field in package.json.

📦 Your First Plop Example

Let's start with a minimal example that generates a Vue component file.

1. Create the Configuration File

Create plopfile.mjs in the project root:

plopfile.mjs
export default function (plop) {
    plop.setGenerator('component', {
        description: 'Create a Vue component',
        prompts: [
            {
                type: 'input',
                name: 'name',
                message: 'Enter the component name'
            }
        ],
        actions: [
            {
                type: 'add',
                path: 'src/components/{{pascalCase name}}.vue',
                templateFile: 'plop-templates/component.hbs'
            }
        ]
    });
}

2. Create the Template File

Create plop-templates/component.hbs:

plop-templates/component.hbs
<template>
    <div class="{{kebabCase name}}">
        {{name}} component
    </div>
</template>

<script setup>
// {{name}} component logic
</script>

<style scoped>
.{{kebabCase name}} {
    /* Component styles */
}
</style>

3. Run the Generator

Run the command:

npx plop component

Plop asks for a component name. Enter UserInfo and press Enter to generate src/components/UserInfo.vue.

The generated content looks like this:

src/components/UserInfo.vue
<template>
    <div class="user-info">
        UserInfo component
    </div>
</template>

<script setup>
// UserInfo component logic
</script>

<style scoped>
.user-info {
    /* Component styles */
}
</style>
{{pascalCase name}} and {{kebabCase name}} are built-in Handlebars helpers provided by Plop. They convert input to PascalCase and kebab-case, respectively. We will cover them in more detail later.

💡 When to Use Plop

Plop is useful when:

  • You frequently create files with the same structure, such as components, pages, or API files.
  • Your team wants consistent code styles and directory structures for new files.
  • A full scaffolding tool is more than you need, and a simple generator is sufficient.

If your project uses scaffolding from Vue CLI, Vite, or Nuxt, you can also use Plop alongside it for more granular, customized file generation.


❓ Questions and Answers

Q1: How does Plop differ from a scaffolding tool?

Scaffolding tools typically create an entire project, as npm create vue does. Plop is lighter and typically generates individual files or groups of files within an existing project.

Q2: Why use plopfile.mjs with Plop 4.x?

Plop 4.x uses ES Modules. The .mjs extension tells Node.js to parse the file as ESM without changing the type field in package.json.

Q3: Can I use Plop without installing it in my project?

You can run it temporarily with npx plop, but installing it in devDependencies is recommended to keep the version consistent across your team.

Q4: What template syntax does Plop use?

Plop uses Handlebars as its default template engine, with syntax such as {{variableName}}.


🧪 Exercise

Complete these steps to create your first Plop generator:

  1. Create a plop-demo project and install plop@4.0.5.
  2. Create plopfile.mjs and plop-templates/component.hbs.
  3. Run npx plop component, enter MyButton, and inspect the generated file.
# Run your commands here

🎉 You have completed the introduction to Plop! Next, we will explore plopfile.mjs configuration in more depth and build a more complete generator.