繁体   English   中英

运行命令 node dist/index.js 给出与 src 文件夹中的 Typescript 文件相关的错误

[英]Running the command node dist/index.js gives an error related to Typescript files in src folder

我有一个 GraphQL 和 Apollo 服务器https://github.com/AdhamAH/Film-and-actors-list/tree/main/server

运行tsc -p. 然后node dist/index.js我从src文件夹中的 TS 文件中得到错误,如下所示

Film-and-actors-list\server\src\entities\Films.ts:1

import {Field, ObjectType} from "type-graphql";
^^^^^^

SyntaxError: Cannot use import statement outside a module

在没有运气的情况下搜索了几个小时后,我想到了删除src文件夹,当我运行node dist/index.js时,服务器启动时没有错误

我试图更改tsconfig.json选项但没有运气

我的tsconfig

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": ["dom", "es6", "es2017", "esnext"],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
    "removeComments": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "resolveJsonModule": true,
    "baseUrl": "."
  },
  "exclude": ["node_modules"],
  "include": ["./src/**/*.ts"]
}

还有我的package.json

{
  "name": "film-actor-list-backend",
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "dev": "nodemon -w src --ext ts --exec ts-node src/index.ts",
    "build": "tsc -p .",
    "start": "node dist/index.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/AdhamAH/Film-and-actors-list.git"
  },
  "author": "A.AboHasson",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/AdhamAH/Film-and-actors-list/issues"
  },
  "homepage": "https://github.com/AdhamAH/Film-and-actors-list#readme",
  "devDependencies": {
    "@types/connect-redis": "^0.0.17",
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/express-session": "^1.17.4",
    "@types/node": "^16.11.7",
    "@types/redis": "^2.8.32",
    "apollo-server-core": "^3.5.0",
    "nodemon": "^2.0.15",
    "ts-node": "^10.4.0",
    "typescript": "^4.5.2"
  },
  "dependencies": {
    "apollo-server-express": "^3.5.0",
    "connect-redis": "^6.0.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "express-session": "^1.17.2",
    "graphql": "15.3.0",
    "redis": "^3.1.2",
    "reflect-metadata": "^0.1.13",
    "sqlite3": "^5.0.2",
    "type-graphql": "^1.1.1",
    "typeorm": "^0.2.40",
    "typeorm-encrypted": "^0.6.0"
  }
}

我想到了

问题是我有一个文件ormconfig.json如下

{
  "type": "sqlite",
  "database": "./db.sqlite3",
  "entities": ["./src/entities/*.ts"],
  "synchronize": true
}

所以看起来当我启动服务器时它正在从entities文件夹加载 TS 文件

我做了什么,我像这样将此配置添加到index.ts

const connection = await createConnection({
      "type": "sqlite",
      "database": "./db.sqlite3",
      "entities": [User, Films],
      "synchronize": true
  })

有了这个我导入了实体并解决了问题

暂无
暂无

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

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