Node js + TypeS c + Babel "Ошибка: не удается найти модуль 'koa'" - PullRequest
0 голосов
/ 25 апреля 2020
  1. Я клонировал демо 1003 *. после установки коа-роутера et c я получил ошибку. Вот мой файл index.ts
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"))

Вот мой пакет. 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"
  }
}

моя структура файла выглядит примерно так:

/-------
  -lib
    -index.js
    -index.d.ts
  -node_modules
  -src
    -index.ts
На самом деле, перед этим, когда я запускаю npm run build, у меня появляется ошибка src/index.ts:2:20 - error TS2307: Cannot find module 'koa-router'. Я пишу koa-router.d.ts сам, но я не думаю, что это отличная идея, как вы, ребята, решаете?

struct

...