4

When I try to run my compiled typescript code I get a syntax error:

\entity\Config.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";
                                                                     ^

SyntaxError: Unexpected token { 

but when I run the typescript code with ts-node and nodemon code runs just fine.

So I've worked on some logging to figure out where the problem is occurring and it seems to happen when I hit createConnection() method on TypeORM. I'm new to Typescript and the TypeORM library.

entity/config.ts

import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm";

@Entity()
export class Config extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  app: String;

  @Column()
  endpoint: String;

  @Column()
  token: String;
}

server.ts

import { createConnection } from "typeorm";

// Database connected
createConnection()
  .then(() => {
    console.log("Test");
  })
  .catch(err => {
    console.log(err);
  });

index.ts

require("reflect-metadata");
require("dotenv/config");
require("./server");

package.json dependancies

    "scripts": {
    "dev:server": "ts-node src",
    "dev": "nodemon -e ts -w src -x npm run dev:server",
    "build:server": "tsc",
    "start:server": "node build/index.js",
    "start": "npm run build:server && npm run start:server"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "devDependencies": {
    "@types/axios": "^0.14.0",
    "@types/graphql": "^14.0.3",
    "@types/node": "^10.12.18",
    "@types/winston": "^2.4.4",
    "nodemon": "^1.18.9",
    "ts-node": "^7.0.1",
    "typescript": "^3.2.2"
  },
  "dependencies": {
    "apollo-server-express": "^2.3.1",
    "axios": "^0.18.0",
    "dotenv": "^6.2.0",
    "express": "^4.16.4",
    "graphql": "^14.0.2",
    "pg": "^7.7.1",
    "reflect-metadata": "^0.1.12",
    "sequelize": "^4.42.0",
    "typeorm": "^0.2.9",
    "winston": "^3.1.0"
  }
}
6
  • 1
    Please post the necessary code required for someone to help you out with your question. You have not provided any code that someone could run in order to try to reproduce your issue. Commented Dec 27, 2018 at 1:27
  • You are right I'm sorry for not providing more information. While was simplifying the code into something easy to reproduce I got information from the error so thank you :D Commented Dec 27, 2018 at 3:39
  • What version of nodejs, typescript, and typeorm are you running? Commented Dec 27, 2018 at 5:59
  • Node: 10.14.2 Typescript:3.2.2 TypeORM: 0.2.9 All these should be the latest stables Commented Dec 28, 2018 at 15:12
  • @JustinRhoades did you solve your problem? Please, add also how you run your code (npm scripts or just commands) it will help Commented Dec 29, 2018 at 14:32

3 Answers 3

7

So a member on the TypeORM Slack (going by uladzimir as of answering this question) solved the problem. The issue was with my ormconfig file.

Problem:

"entities": ["src/database/entity/**/*.ts", "build/database/entity/**/*.js"],
  "migrations": [
    "src/database/migration/**/*.ts",
    "build/database/migration/**/*.js"
  ],
  "subscribers": [
    "src/database/subscriber/**/*.ts",
    "build/database/subscriber/**/*.js"
  ],
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
  }

Solution:

"entities": ["build/database/entity/**/*.js"],
  "migrations": ["build/database/migration/**/*.js"],
  "subscribers": ["build/database/subscriber/**/*.js"],
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
  }

For some reason I thought I had to add the *ts files for testing/developing purposes but that is not the case and what was causing the issue. I'm not exactly sure why it would be causing this problem but if I find out I'll post that as a comment to this answer.

Thank you everyone for the help on this issue.

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

1 Comment

I'm glad I could help :)
2

i managed to solve this issue by deleting the ormconfig.json file and passing the database config in the createConnection function.

example:

import { User } from './entity'
// import every other entity you have
// .......

await createConnection({
        type: 'sqlite',
        database: 'database.sqlite',
        synchronize: true,
        logging: true,
        entities: [
            User // pass your entities in here
        ]
    })

1 Comment

Great! This way, you don't need to transpile .ts entities into .js
2

If you are using TypeOrm you could use this as your config

  entities: [__dirname + '/models/**/*.entity.{ts,js}'],
  migrations: [__dirname + '/migrations/**/*.{ts,js}'],
  subscribers: [__dirname + '/subscriber/**/*.{ts,js}'],

Adding __dirname correctly resolves the modules depending on if you're running it in dev (resolved to ts version) or prod (resolved to js version)

Comments

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.