• Version 7.0.0
  • Published
  • 9.61 kB
  • No dependencies
  • MIT license


npm i detect-indent
yarn add detect-indent
pnpm add detect-indent


Detect the indentation of code



function detectIndent

detectIndent: (string: string) => Indent;
  • Detect the indentation of code.

    Parameter string

    A string of any kind of text.

    Example 1

    import fs from 'node:fs';
    import detectIndent from 'detect-indent';
    // {
    // "ilove": "pizza"
    // }
    const file = fs.readFileSync('foo.json', 'utf8');
    // Tries to detect the indentation and falls back to a default if it can't
    const indent = detectIndent(file).indent || ' ';
    const json = JSON.parse(file);
    json.ilove = 'unicorns';
    fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));
    // {
    // "ilove": "unicorns"
    // }


interface Indent

interface Indent {}

    property amount

    amount: number;
    • The amount of indentation. For example, 2.

    property indent

    indent: string;
    • The actual indentation.

    property type

    type: 'tab' | 'space' | undefined;
    • The type of indentation.

      It is undefined if no indentation is detected.

    Package Files (1)

    Dependencies (0)

    No dependencies.

    Dev Dependencies (4)

    Peer Dependencies (0)

    No peer dependencies.


    To add a badge like this badgeto your package's README, use the codes available below.

    You may also use to create a custom badge linking to

    • Markdown
    • HTML
      <a href=""><img src="" alt=""></a>