Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

plop

> 开发工具
Open source

Consistency Made Simple

7.7K stars0 likes0 views
WebsiteGitHub

About

Consistency Made Simple

Plop

Micro-generator framework that makes it easy for an entire team to create files with a level of uniformity.

Documentation also available on plopjs.com

Getting Started

 

What is Plop?

Plop is what I like to call a "micro-generator framework." Now, I call it that because it is a small tool that gives you a simple way to generate code or any other type of flat text files in a consistent way. You see, we all create structures and patterns in our code (routes, controllers, components, helpers, etc). These patterns change and improve over time so when you need to create a NEW insert-name-of-pattern-here, it's not always easy to locate the files in your codebase that represent the current "best practice." That's where plop saves you. With plop, you have your "best practice" method of creating any given pattern in CODE. Code that can easily be run from the terminal by typing plop. Not only does this save you from hunting around in your codebase for the right files to copy, but it also turns "the right way" into "the easiest way" to make new files.

If you boil plop down to its core, it is basically glue code between inquirer prompts and handlebar templates.

This documentation is a work in progress. If you have great ideas, I'd love to hear them.

Installation

1. Add plop to your project

$ npm install --save-dev plop

2. Install plop globally (optional, but recommended for easy access)

$ npm install -g plop

3. Create a plopfile.js at the root of your project

javascript
export default function (plop) {
  // create your generators here
  plop.setGenerator("basics", {
    description: "this is a skeleton plopfile",
    prompts: [], // array of inquirer prompts
    actions: [], // array of actions
  });
}

export default is only allowed in NodeJS inside "ESM" supported files. To use this syntax, your plopfile must be either:

  • An ESM .js file with type: "module" in package.json
  • An ESM .mjs file with any type declared in package.json

Alternatively, you can have a plopfile with module.exports = function (plop) instead. For this syntax, your plopfile must be either:

  • A CommonJS .js file with type: "commonjs" in package.json
  • A CommonJS .cjs file with any type declared in package.json

Your First Plopfile

A plopfile starts its life as a node module that exports a function which accepts the plop object as its first parameter.

javascript
export default function (plop) {}

The plop object exposes the plop API object which contains the setGenerator(name, config) function. This is the function that you use to (wait for it) create a generator for this plopfile. When plop is run from the terminal in this directory (or any sub-directory), a list of these generators will be displayed.

Let's try setting up a basic generator to see how that looks.

javascript
export default function (plop) {
  // controller generator
  plop.setGenerator("controller", {
    description: "application controller logic",
    prompts: [
      {
        type: "input",
        name: "name",
        message: "controller name please",
      },
    ],
    actions: [
      {
        type: "add",
        path: "src/{{name}}.js",
        templateFile: "plop-templates/controller.hbs",
      },
    ],
  });
}

The controller generator we created above will ask us 1 question, and create 1 file. This can be expanded to ask as many questions as needed, and create as many files as needed. There are also additional actions that can be used to alter our codebase in different ways.

Using Prompts

Plop uses the inquirer.js library to gather user data. A list of prompt types can be found on the inquirer official website.

CLI Usage

Once plop is installed, and you have created a generator, you are ready to run plop from the terminal. Running plop with no parameters will present you with a list of generators to pick from. You can also run plop [generatorName] to trigger a generator directly. If you did not install plop globally, you will need to setup an npm script to run plop for you.

javascript
// package.json
{
    ...,
    "scripts": {
        "plop": "plop"
    },
    ...
}

Bypassing Prompts

Once you get to know a project (and its generators) well, you may want to provide answers to the prompts when you run the generator. If I have (for instance) a component generator that has one prompt (name), I can run that generator using plop component "some component name" and it will immediately execute as though I had typed "some component name" into the prompt. If that same generator had a second prompt, the same input would have resulted in the user being prompted for the second value.

Prompts like confirm and list try to make sense of your input as best they can. For instance entering "y", "yes", "t", or "true" for a confirm prompt will result in a boolean true value. You can select items from a list using their value, index, key, or name. Checkbox prompts can accept a comma separated list of values in order to select multiples.

If you want to provide bypass input for the second prompt but not the first, you can use an underscore "_" to skip the bypass (ie plop component _ "input for second prompt").

Plop comes with bypass logic built-in for standard inquirer prompts, but there are also ways to provide custom logic for how to handle user input for a specific prompt.

If you have published a 3rd party inquirer prompt plugin and would like to support bypass functionality for plop users out of the box, that is covered in another section of this documentation.

Bypassing Prompts (by Name)

You can also bypass prompts by name using -- and then providing arguments for each prompt that you'd like to bypass. Examples below.

Bypass Examples

## Bypassing both prompt 1 and 2
$ plop component "my component" react
$ plop component -- --name "my component" --type react

## Bypassing only prompt 2 (will be prompted for name)
$ plop component _ react
$ plop component -- --type react

Running a Generator Forcefully

By default Plop actions keep your files safe by failing when things look fishy. The most obvious example of this is not allowing an add action to overwrite a file that already exists. Plop actions individually support the force property but you can also use the --force flag when running Plop from the terminal. Using the --force flag will tell every action to run forcefully. With great power...

Using TypeScript plopfiles

Node.js v22.18+**

Modern Node.js supports TypeScript files out-of-the-box. No extra setup or CLI flags are required to use a TypeScript plopfile.

First, make a TypesScript plopfile using plop --init-ts or by hand:

typescript
// plopfile.ts
import type { NodePlopAPI } from "plop";

export default function (plop: NodePlopAPI) {
  // plop generator code
}

Second, add plop to scripts in package.json:

json
// package.json
"scripts": {
  "plop" : "plop"
}

Older Node.js Versions**

Plop bundles TypeScript declarations and supports TypeScript plopfiles via tsx loaders, a feature of NodeJS command line imports.

First, make a TypesScript plopfile using plop --init-ts or by hand:

typescript
// plopfile.ts
import { NodePlopAPI } from "plop";

export default function (plop: NodePlopAPI) {
  // plop generator code
}

Next, install tsx and optionally cross-env:

bash
npm i -D tsx cross-env

Finally, use NODE_OPTIONS to activate the tsx loader. Now Plop can import your plopfile.ts:

Node.js v20.6 and above

json
// package.json
"scripts": {
  "plop" : "cross-env NODE_OPTIONS='--import tsx' plop --plopfile=plopfile.ts"
}

Node.js v20.5.1 and below

json
// package.json
"scripts": {
  "plop": "cross-env NODE_OPTIONS='--loader tsx' plop --plopfile=plopfile.ts"
}

Why Generators?

Because when you create your boilerplate separate from your code, you naturally put more time and thought into it.

Because saving your team (or yourself) 5-15 minutes when creating every route, component, controller, helper, test, view, etc... really adds up.

Because context switching is expensive and saving time is not the only benefit to automating workflows

Plopfile API

The plopfile api is the collection of methods that are exposed by the plop object. Most of the work is done by setGenerator but this section documents the other methods that you may also find useful in your plopfile.

TypeScript Support

Plop bundles TypeScript declarations. See using TypeScript plopfiles for more details.

JSDoc Support

Whether or not you write your plopfile in TypeScript, many editors will offer code assistance via JSDoc declarations.

javascript
// plopfile.js
export default function (
  /** @type {import('plop').NodePlopAPI} */
  plop,
) {
  // plop generator code
}

Main Methods

These are the methods you will commonly use when creating a plopfile. Other methods that are mostly for internal use are list in the other methods section.

Method Parameters Returns Description
setGenerator String, GeneratorConfig PlopGenerator setup a generator
setHelper String, Function setup handlebars helper
setPartial String, String setup a handlebars partial
setActionType String, CustomAction register a custom action type
setPrompt String, InquirerPrompt registers a custom prompt type with inquirer
load Array[String], Object, Object loads generators, helpers and/or partials from another plo

Issues· 91 open

View all issuesOpen on GitHub
  • #500

    security issue -> plop -> node-plop -> handlebars

    Updated Jul 16, 2026
  • #370

    addMany has strange behavior for files with only one extension

    Updated Jul 15, 2026
  • #490

    security issue plop → node-plop → inquirer → external-editor → tmp

    Updated Jan 26, 2026
  • #423

    plopfile.ts Typescript support

    Updated Dec 24, 2025
  • #485

    Bug with backslashes

    Updated Dec 18, 2025
  • #240

    Base path with relative path fails to find templates

    bughelp wantedgood first issueUpdated Oct 23, 2025
  • #467

    Is the type definition of your DynamicPromptsFunction wrong?

    Updated Jul 29, 2025
  • #441

    Async follow up prompts

    Updated May 15, 2025
  • #465

    Append action removes literal "$`" due to special replacement pattern handling

    Updated Mar 29, 2025
  • #445

    [ERROR] String.prototype.split called on null or undefined

    Updated Feb 6, 2025

> Tags

JavaScriptcligeneratorjavascriptplop

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category开发工具
PricingOpen source

> Related tools

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具