new-cap

Require constructor names to begin with a capital letter

A capital first letter is how you tell a constructor from an ordinary function. If names don't follow that convention, it's easy to forget new. A class then throws, and an older constructor function runs with the wrong this.

The rule reports two things: new with a name that starts with a lowercase letter, and a call without new to a name that starts with a capital letter.

The built-ins that work without new are allowed: Array, BigInt, Boolean, Date, Error, Function, Number, Object, RegExp, String, Symbol, and Date.UTC().

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"new-cap" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"new-cap" = ["error", {
  newIsCap = true,
  capIsNew = true,
  properties = true,
  newIsCapExceptions = [], # list of strings
  # newIsCapExceptionPattern = <string>,
  capIsNewExceptions = [], # list of strings
  # capIsNewExceptionPattern = <string>,
}]

Examples#

example.js2 problems
const user = new person("Ada");
const team = Team("core");
example.jsNo problems
const user = new Person("Ada");
const team = createTeam("core");
const id = String(42);
const start = Date.UTC(2026, 0, 1);

Options#

bunfig.toml
[lint.rules]
new-cap = ["error", { capIsNewExceptions = ["Router"] }]
OptionDefaultWhat it does
newIsCaptrueReport new with a lowercase name.
capIsNewtrueReport a call to a capitalized name without new.
propertiestrueAlso check names after a dot, as in new net.socket() and express.Router().
newIsCapExceptions[]Lowercase names that are allowed after new.
newIsCapExceptionPatternnoneA regular expression for those names.
capIsNewExceptions[]Capitalized names that can be called without new.
capIsNewExceptionPatternnoneA regular expression for those names.

Reported by default:

example.js1 problem
const router = express.Router();

Not reported with capIsNewExceptions: ["Router"]. "express.Router" works too:

example.jswith { "capIsNewExceptions": ["Router"] }No problems
const router = express.Router();

A pattern is tested against the whole express.Router. Not reported with capIsNewExceptionPattern: "^express\\.":

example.jswith { "capIsNewExceptionPattern": "^express\\." }No problems
const router = express.Router();

Not reported with newIsCapExceptions: ["moment"]:

example.jswith { "newIsCapExceptions": ["moment"] }No problems
const now = new moment();

Not reported with properties: false:

example.jswith { "properties": false }No problems
const router = express.Router();
const socket = new net.socket();

When not to use it#

Some libraries export capitalized functions that are meant to be called without new. If you use many of them, set capIsNew to false.