microsoft / TypeScript-Babel-Starter

A sample setup using Babel CLI to build TypeScript code, and using TypeScript for type-checking.
MIT License
2k stars 229 forks source link

Error: Cannot find module 'koa' #57

Closed linxingwu closed 4 years ago

linxingwu commented 4 years ago
  1. I have cloned this demo . after installing koa,koa-router etc, I got an error. Here is my index.ts file
import Koa from "koa"
import Router from "koa-router"
import logger from "koa-logger"
import json from "koa-json"

const app = new Koa()
const router = new Router()

router.get("/",async(ctx: any,next: any)=>{
    ctx.body = {
        meg:"Hello world"
    }
    await next
})

app.use(logger())
app.use(json())
app.use(router.routes()).use(router.allowedMethods())

app.listen(3000,()=>console.log("app is running at 3000"))

Here is my package.json

{
  "name": "babel-typescript-sample",
  "version": "0.7.2",
  "license": "MIT",
  "scripts": {
    "type-check": "tsc --noEmit",
    "type-check:watch": "npm run type-check -- --watch",
    "build": "npm run build:types && npm run build:js",
    "build:types": "tsc --emitDeclarationOnly",
    "build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline"
  },
  "devDependencies": {
    "@babel/cli": "^7.8.3",
    "@babel/core": "^7.8.3",
    "@babel/plugin-proposal-class-properties": "^7.8.3",
    "@babel/preset-env": "^7.8.3",
    "@babel/preset-typescript": "^7.8.3",
    "@types/koa": "^2.11.3",
    "typescript": "^3.7.5"
  },
  "dependencies": {
    "koa": "^2.11.0",
    "koa-bodyparser": "^4.3.0",
    "koa-json": "^2.0.2",
    "koa-logger": "^3.2.1",
    "koa-router": "^8.0.8"
  }
}

my file structure loos like:

/-------
  -lib
    -index.js
    -index.d.ts
  -node_modules
  -src
    -index.ts
  1. Actually,before this ,when I run npm run build,I got error src/index.ts:2:20 - error TS2307: Cannot find module 'koa-router'. I write 'koa-router.d.ts' myself, but I don't think it's a great idea, How do you guys resolve?
linxingwu commented 4 years ago

There are the same reason, could find declare file just run npm npm i @types/<package>