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

标签 node.js typescript typeorm

我有一个 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
  })

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

关于node.js - 运行命令 node dist/index.js 会出现与 src 文件夹中的 Typescript 文件相关的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70142868/

相关文章:

typescript - NestJS TypeORM 模拟存储库的数据源

javascript - 从 NPM 安装 CRA 和 NextJS 时出现问题(错误 : Couldn't find package "@babel/core" on "npm" registry)

typescript - 错误: [Vue warn]: Invalid handler for event "click": got undefined

eclipse - nodeclipse 中有单元测试集成吗?

typescript - 定义 1...n 个字符串模板类型的通用交集类型

typescript - 在哪里可以找到 TypeScript 的内置类型和标准库的文档?

javascript - TypeORM 返回普通对象

node.js - NestJS - 无法将服务注入(inject)订阅者

node.js - 导出mongodb集合数据并使用node js导入回来

javascript - 前端 JS 和后端 JS 之间更好的性能