繁体   English   中英

在节点上的 typescript 错误中找不到本地模块

[英]Cannot find local module in typescript error on node

我是 typescript 的新手,我的问题是我创建的本地模块无法使用

./route/routes.ts

import routes from 'next-routes'
                           
let route = new routes()

route.add('index', '/:name')

export default route

在我的 server.ts 中

import route from "./route/routes"

但是当我运行npm run start然后我得到以下错误

找不到模块“./route/routes”

这是我的 tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ]
}

和我的 package.json

{
  "name": "Test",
  "version": "1.0.0",
  "description": "",
  "main": "babel-node --presets es2015 server.ts",
  "scripts": {
    "dev": "node server.ts",
    "build": "next build",
    "postinstall": "npm run build",
    "start": "NODE_ENV=production babel-node --presets es2015 server.ts"
  },
  "author": "Test",
  "license": "",
  "dependencies": {
    "@zeit/next-sass": "^1.0.1",
    "express": "^4.17.1",
    "next": "^9.0.5",
    "next-routes": "^1.4.2",
    "node-sass": "^4.12.0",
    "react": "^16.9.0",
    "react-dom": "^16.9.0"
  },
  "devDependencies": {
    "@types/express": "^4.17.11",
    "@types/node": "^14.14.31",
    "@types/react": "^17.0.2",
    "babel": "^6.23.0",
    "babel-cli": "^6.26.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "nextjs-sitemap-generator": "^0.1.1",
    "nodemon": "^2.0.7",
    "typescript": "^4.2.2"
  }
}

我从 js 迁移到 typescript 后出现错误

注意:我尝试在网上寻找解决方案搜索似乎找不到真正好的解决方案

谢谢

尝试将 routes.ts 移动到 server.ts 文件所在的任何位置,更新 import 语句,看看它是否有效

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM