Installiere
Bevor Sie beginnen, stellen Sie sicher, dass Sie Node.js 18 oder höher installiert haben. Erstellen Sie dann ein Verzeichnis für Ihre Anwendung und navigieren Sie hinein.
mkdir myappcd myappBenutze den Befehl npm init um eine package.json Datei für deine Anwendung zu erstellen.
Für weitere Informationen darüber, wie package.json funktioniert, siehe Spezifikationen für npm’s package.json handling.
npm inityarn initpnpm initbun initDieser Befehl fragt Sie nach einer Reihe von Dingen wie dem Namen und der Version Ihrer Anwendung. Im Moment können Sie einfach RETURN drücken, um die Standardwerte für die meisten von ihnen zu akzeptieren, mit der folgenden Ausnahme:
entry point: (index.js)Geben Sie app.js ein, oder was auch immer der Name der Hauptdatei sein soll. Wenn Sie index.js wollen, drücken Sie RETURN um den empfohlenen Dateinamen zu akzeptieren.
Installieren Sie jetzt Express im myapp Verzeichnis und speichern Sie es in der Abhängigkeitsliste. Zum Beispiel:
npm install expressyarn add expresspnpm add expressbun add expressUm Express vorübergehend zu installieren und nicht zur Abhängigkeitsliste hinzufügen:
npm install express --no-savebun add express --no-saveTypeScript
Express is written in JavaScript and does not bundle its own type definitions. To use it with TypeScript, install TypeScript together with the community-maintained types for Express and Node.js (from DefinitelyTyped) as development dependencies:
npm install --save-dev typescript @types/express @types/nodeyarn add --dev typescript @types/express @types/nodepnpm add --save-dev typescript @types/express @types/nodebun add --dev typescript @types/express @types/nodeSome middleware does not bundle its own type definitions. If you add an official middleware package
that TypeScript reports as untyped, also install its types from DefinitelyTyped as a dev dependency,
for example @types/cors alongside cors.
Add a tsconfig.json. These options mirror how Node.js runs TypeScript and make the compiler reject
non-erasable syntax (such as enums, namespaces, and parameter properties) that Node cannot strip:
{ "compilerOptions": { "target": "esnext", "module": "nodenext", "rewriteRelativeImportExtensions": true, "erasableSyntaxOnly": true, "verbatimModuleSyntax": true, "noEmit": true, "strict": true, "skipLibCheck": true }}Write your application in TypeScript, annotating the request and response objects:
import express, { type Express, type Request, type Response } from 'express';
const app: Express = express();
app.get('/', (req: Request, res: Response) => { res.send('Hello World!');});
app.listen(3000);You do not need to annotate everything. When you pass a handler directly to a route method or to
app.use(), Express infers the types of req, res, and next, and it infers route parameters
from the path, so req.params.id is a string in app.get('/users/:id', ...). Add explicit types
only where TypeScript has no context to infer from: error-handling middleware, whose
(err, req, res, next) signature is not inferred, and handlers you define separately from the route.
In those cases, annotate the parameters or type the whole function as RequestHandler or
ErrorRequestHandler.
Run the file directly with Node.js, which strips the TypeScript types and runs the result without a build step:
node src/app.tsNote
Running .ts files directly requires Node.js >= 22.18.0 (or >= 23.6.0 on the v23 line) and
TypeScript >= 5.8. Node strips the types but does not type-check them, so run npx tsc to type-check
your project. For more details, see the Node.js guide on
running TypeScript natively.