2

I have a GraphQL and Apollo server

After running tsc -p . then node dist/index.js I am getting the error from a TS file in the src folder like this

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

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

SyntaxError: Cannot use import statement outside a module

After searching for hours with no luck I had an idea to delete the src folder and when I run node dist/index.js the server starts with no error

I tried to change the tsconfig.jsonoptions but no luck

My 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"]
}

And my 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"
  }
}

1 Answer 1

2

I figured it out

The problem was that I had a file ormconfig.json with following

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

So it looks like that when I start the server it is loading the TS files from entities folder

What I did that I added this configuration to index.ts like this

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

With this I imported the entities and solved the problem

Sign up to request clarification or add additional context in comments.

1 Comment

I just changed "entities": ["./src/entities/*.ts"], from ormconfig.json to "entities": ["./dist/entities/*.js"], , and it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.