Skip to content

Instantly share code, notes, and snippets.

@kamiyam kamiyam/
Last active Apr 20, 2018

What would you like to do?
migrate express js2ts
  • code 一式

  • init
mkdir project
cd project
npx express-generator src
mv src/package.json package.json
npm i -D typescript tslint ts-node @types/node @types/express @types/cookie-parser @types/http-errors @types/debug

npx tsc --init
npx tslint --init

mv src/app.js src/app.ts
mv src/routes/index.js src/routes/index.ts
mv src/routes/users.js src/routes/users.ts
mv src/bin/www src/bin/www.ts

mkdir dist

cp src/public dist/public
cp src/views dist/views
  • edit package.json
"scripts": {
	"start": "PORT=3333 node ./dist/bin/www",
	"serve": "ts-node ./src/bin/www.ts",	
	"build": "tsc",
	"lint": "tslint src/**/*.ts"
  • edit tsconfig.json
		"target": “ES2017”,
		"outDir": "./dist",
		"lib": [
		"sourceMap": true,
		"rootDir": "./src"
	"include": [
	"exclude": [
  • replace code
import express from 'express';
// import {default as express, Request, Response, NextFunction} from 'express';
function onError(error: any) {
app.use(function(req: express.Request, res: express.Response, next: express.NextFunction) {
function normalizePort(val: any): number|string|boolean {
  • build
npm run build
  • server
npm run serve
# access http://localhost:3000

npm run build
npm start 
# access http://localhost:3333
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.