Skip to content

Instantly share code, notes, and snippets.

@mcconkiee
Created October 22, 2019 22:12
Show Gist options
  • Save mcconkiee/f1ebed94940004f19dcc8ea96cc9ca1e to your computer and use it in GitHub Desktop.
Save mcconkiee/f1ebed94940004f19dcc8ea96cc9ca1e to your computer and use it in GitHub Desktop.
Typescript quick setup
# https://dev.to/theghostyced/setting-up-a-node-typescript-project-in-under-4-minutes-4gk2
npm init -y
# ts config
touch tsconfig.json
echo '{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"rootDir": "src",
"outDir": "dist",
"sourceMap": true,
"resolveJsonModule": true,
"lib": ["es6", "dom"],
"esModuleInterop": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}' > tsconfig.json
# index
mkdir src && touch src/app.ts
echo "import Koa from \"koa\";
import Router from 'koa-router';
const router = new Router()
const dotenv = require(\"dotenv\");
dotenv.config();
const app = new Koa();
router.get(\"/\", ctx => {
ctx.body = \"hello\";
});
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);
export default app;
" > src/app.ts
# dependencies
npm i -D typescript
npm i -D ts-node
npm i -D nodemon
npm i dotenv koa koa-router koa-body --save
npm i -D @types/koa @types/dotenv @types/koa-router
echo "modify package.json to contain"
echo "\"scripts\": {
\"start\": \"tsc && node dist/index.js\",
\"dev\": \"nodemon --watch 'src/**/*.ts' --exec 'ts-node' .src/app.ts\",
\"test\": \"echo \"Error: no test specified\" && exit 1\"
},"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment