【问题标题】:Running the command node dist/index.js gives an error related to Typescript files in src folder运行命令 node dist/index.js 给出与 src 文件夹中的 Typescript 文件相关的错误
【发布时间】:2022-01-05 14:30:35
【问题描述】:

我有一个 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.jsonoptions 但没有成功

我的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"
  }
}

【问题讨论】:

    标签: node.js typescript typeorm


    【解决方案1】:

    我想通了

    问题是我有一个文件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
      })
    

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

    【讨论】:

    • 我刚刚将 "entities": ["./src/entities/*.ts"],ormconfig.json 更改为 "entities": ["./dist/entities/*.js"], ,它工作正常。
    猜你喜欢
    • 1970-01-01
    • 2018-08-06
    • 2020-07-30
    • 2017-07-09
    • 2021-03-26
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多