question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native

TypeORM version:

[ ] latest [ ] @next [x] 0.2.5 (or put your version here)

First of all, thanks for the awesome ORM. When using TypeScript this is by far the superior solution!

So, the issue I am having is that everything works when I am running my project using ts-node, but when I build the project and transpile to JS, I get import errors on my entity files.

I also created an issue on StackOverflow

Error:

(function (exports, require, module, __filename, __dirname) { import { Entity, PrimaryGeneratedColumn, ManyToOne, OneToMany, TreeChildren, TreeParent, JoinColumn, Column, Tree, TreeLevelColumn } from "typeorm";
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:616:28)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Function.PlatformTools.load (C:\Users\DDeWulf\Workspace\experimental\rabbly\node_modules\typeorm\platform\PlatformTools.js:126:28)

My package.json:

{
   "name": "rabbly",
   "version": "1.0.0",
   "description": "",
   "main": "index.js",
   "scripts": {
      "test": "echo \"Error: no test specified\" && exit 1",
      "dev": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec ts-node src/index.ts",
      "start": "tsc && node ./dist/index.js",
      "migrate": "ts-node ./node_modules/typeorm/cli.js migration:generate"
   },
   "author": "David DeWulf",
   "license": "ISC",
   "dependencies": {
      "bcryptjs": "^2.4.3",
      "body-parser": "^1.18.3",
      "class-validator": "^0.8.5",
      "express": "^4.16.3",
      "jwt-simple": "^0.5.1",
      "morgan": "^1.9.0",
      "pg": "^7.4.3",
      "reflect-metadata": "^0.1.10",
      "typeorm": "0.2.5"
   },
   "devDependencies": {
      "@types/bcryptjs": "^2.4.1",
      "@types/body-parser": "^1.17.0",
      "@types/express": "^4.11.1",
      "@types/jwt-simple": "^0.5.33",
      "@types/node": "^8.10.15",
      "ts-node": "3.3.0",
      "typescript": "2.5.2"
   }
}

My ormconfig.json:

{
   "type": "postgres",
   "host": "***********************",
   "port": 5432,
   "username": "***********************",
   "password": "*****************",
   "database": "***********************",
   "synchronize": true,
   "logging": false,
   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}

And the file throwing the error:

import { Entity, PrimaryGeneratedColumn, ManyToOne, OneToMany, TreeChildren, TreeParent, JoinColumn, Column, Tree, TreeLevelColumn } from "typeorm";
import { User } from "./User";
import { Debate } from "./Debate";


@Entity()
@Tree("closure-table")
export class Comment {

    @PrimaryGeneratedColumn("uuid")
    id: string;

    @Column()
    text: string;

    @ManyToOne(type => User)
    user: User;

    @ManyToOne(type => Debate, debate => debate.comments)
    debate: Debate;

    @TreeChildren()
    children: Comment[];

    @TreeParent()
    parent: Comment;
}

After playing around some, it seems that the issue is related to when I create a connection to my DB. When I comment out where I create the connection, no more errors are thrown.

The code in my index.ts:

import "reflect-metadata";
import { createConnection } from "typeorm";
import { User } from "./entity/User";
import { userRouter } from './routes/userRoutes';
import * as bodyParser from 'body-parser'
import * as express from 'express';
import { debateRouter } from "./routes/debateRoutes";
import { commentRouter } from "./routes/commentRoutes";

const app: express.Application = express();
const PORT = process.env.PORT || 3000;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use('/user', userRouter);
app.use('/debate', debateRouter);
app.use('/comment', commentRouter);

createConnection().then(connection => {
    app.listen(PORT, () => {
        console.log(`Serving app on port ${PORT}`);
    });
}).catch(error => console.log(error));

Thank you in advance for any and all help.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11

github_iconTop GitHub Comments

33reactions
dwdewulcommented, May 16, 2018

Alright, I am simply dumb.

So, the ormconfig.json points your entities to your ts files in your /src folder. When you build your project, it should point to where your entities are. I did think it was odd that it was trying to reference a TS file after building.

ormconfig.json

{
   "type": "postgres",
   "host": "***********************",
   "port": 5432,
   "username": "***********************",
   "password": "***********************",
   "database": "***********************",
   "synchronize": true,
   "logging": false,
   "entities": [

      "dist/entity/*.js"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],
   "cli": {
      "entitiesDir": "src/entity",
      "migrationsDir": "src/migration",
      "subscribersDir": "src/subscriber"
   }
}
5reactions
rpmonteirocommented, Sep 10, 2019

Using ENV variables, the fix for me was:

TYPEORM_ENTITIES=dist/**/*.entity.js
TYPEORM_MIGRATIONS=dist/migration/*.js
TYPEORM_MIGRATIONS_DIR=dist/migration

took me 2h to get it working again…

Hope this helps some other poor soul

Read more comments on GitHub >

github_iconTop Results From Across the Web

Review and troubleshoot import errors
Below, learn how to resolve errors detected during an import, download an error file, view error details, and correct issues in your import...
Read more >
Handle import errors - Analytics Help - Google Support
Troubleshoot Data Import problems.Not seeing the imported data appearing in your reports, as expected? This article can help you troubleshoot the problem.
Read more >
6 Common CSV Import Errors and How to Fix Them - Flatfile
One of the most common CSV import errors is that the file is simply too large. That can be caused by too many...
Read more >
Best Practices for Fixing SOLIDWORK Import Errors | Fictiv
The last thing you want to happen is to encounter a SOLIDWORKS imported geometry error or other import errors when trying to work...
Read more >
Resolve Import Errors - Pearson Assessment Support
From the main menu, click (or tap) Imports/Exports. A data import list appears. Select only the one file for which you want to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found