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.

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

See original GitHub issue

I’m submitting a bug relationed with entities.ts


[ ] Regression 
[x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Running npm run start, i get:

C:\Projetos\project-name\src\api\user\user.entity.ts:1 [0] import { Entity, Column, PrimaryGeneratedColumn } from ‘typeorm’; [0] ^ [0] [0] SyntaxError: Unexpected token { [0] at Module._compile (internal/modules/cjs/loader.js:721:23) [0] at Object.Module._extensions…js (internal/modules/cjs/loader.js:787:10) [0] at Module.load (internal/modules/cjs/loader.js:653:32) [0] at tryModuleLoad (internal/modules/cjs/loader.js:593:12) [0] at Function.Module._load (internal/modules/cjs/loader.js:585:3) [0] at Module.require (internal/modules/cjs/loader.js:690:17) [0] at require (internal/modules/cjs/helpers.js:25:18) [0] at Function.PlatformTools.load (C:\Projetos\project-name\node_modules\typeorm\platform\PlatformTools.js:107:28) [0] at C:\Projetos\project-name\node_modules\typeorm\util\DirectoryExportedClassesLoader.js:30:69 [0] at Array.map (<anonymous>)

Expected behavior

Expected to run and interpret entity file, connect to database and run migrations or create tables for example.

Minimal reproduction of the problem with instructions

$ npm i -g @nestjs/cli $ nest new project-name

Next, follow these instructions: https://docs.nestjs.com/

Environment


Nest version: 6.5.0
 
For Tooling issues:
- Node version: v10.16.0
- Platform:  Windows

Others:
*NPM*
{
  "name": "project-name",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "license": "MIT",
  "scripts": {
    "build": "tsc -p tsconfig.build.json",
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "concurrently --handle-input \"wait-on dist/main.js && nodemon\" \"tsc -w -p tsconfig.build.json\" ",
    "start:debug": "nodemon --config nodemon-debug.json",
    "prestart:prod": "rimraf dist && npm run build",
    "start:prod": "node dist/main.js",
    "lint": "tslint -p tsconfig.json -c tslint.json",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nest-modules/mailer": "^1.1.3",
    "@nestjs/common": "^6.0.0",
    "@nestjs/core": "^6.0.0",
    "@nestjs/jwt": "^6.1.1",
    "@nestjs/passport": "^6.1.0",
    "@nestjs/platform-express": "^6.0.0",
    "@nestjs/swagger": "^3.0.2",
    "@nestjs/typeorm": "^6.0.0",
    "@sentry/node": "^5.5.0",
    "@types/bcrypt": "^3.0.0",
    "@types/helmet": "0.0.43",
    "@types/passport-jwt": "^3.0.1",
    "bcrypt": "^3.0.6",
    "class-transformer": "^0.2.3",
    "class-validator": "^0.9.1",
    "helmet": "^3.18.0",
    "nestjs-config": "^1.4.2",
    "passport": "^0.4.0",
    "passport-jwt": "^4.0.0",
    "pg": "^7.11.0",
    "randomatic": "^3.1.1",
    "reflect-metadata": "^0.1.12",
    "rimraf": "^2.6.2",
    "rxjs": "^6.3.3",
    "typeorm": "^0.2.16"
  },
  "devDependencies": {
    "@nestjs/testing": "^6.0.0",
    "@types/express": "^4.16.0",
    "@types/jest": "^23.3.13",
    "@types/node": "^10.12.18",
    "@types/nodemailer": "^6.2.0",
    "@types/supertest": "^2.0.7",
    "concurrently": "^4.1.0",
    "jest": "^23.6.0",
    "nodemon": "^1.18.9",
    "prettier": "^1.15.3",
    "supertest": "^3.4.1",
    "ts-jest": "24.0.2",
    "ts-node": "8.1.0",
    "tsconfig-paths": "3.8.0",
    "tslint": "5.16.0",
    "typescript": "3.4.3",
    "wait-on": "^3.2.0"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
zhongyuanjiacommented, Jul 12, 2019

Excuse me that my English isn’t very good.

@Joebob12 you can’t set entities option in ormconfig.json. in production mode, the ts file will compiler to js and extract to dist directory.

problem

your ormconfig.json’s entities option always point to src/**/*.entity.ts.

how to solve

set entities option in your code

import * as path from 'path';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [TypeOrmModule.forRoot({
    ...your options,
    entities: [path.join(__dirname, "**/*.entity{.ts,.js}")]
  })]
})
export class AppModule {}

also, you can use config service or ormconfig.js to solve this problem

4reactions
Joebob12commented, Jul 11, 2019

Just want to bump this up. I’m having this same issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SyntaxError: Unexpected token import TypeORM entity
I have tried changing the import statements in my entity files from import (lib) from (module) to const (lib) require (module)"; However, this ......
Read more >
Entities - typeorm - GitBook
You can create an entity by defining a new class and mark it with @Entity() : import { Entity, PrimaryGeneratedColumn, Column } from...
Read more >
TypeORM - Entity - Tutorialspoint
TypeORM - Entity, An entity is a collection of fields and associated database ... import {Entity, PrimaryGeneratedColumn, Column} from "typeorm"; @Entity() ...
Read more >
How to use the typeorm.PrimaryGeneratedColumn function in ...
import { Column, createConnection, Entity, PrimaryGeneratedColumn} from "typeorm"; @Entity() export class FooModel { @PrimaryGeneratedColumn("uuid") public id: ...
Read more >
TypeORM - Amazing ORM for TypeScript and JavaScript (ES7 ...
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm" @Entity() export class User { @PrimaryGeneratedColumn() id: number @Column() firstName: ...
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