Nest: Problem with hotreload and typeorm - Unexpected token

Created on 13 Jun 2019  路  11Comments  路  Source: nestjs/nest

Bug Report

Current behavior

I cloned the typescript repo tutorial and followed it completely. I also created a few of my own entities and it works without errors.
I then proceeded to introduce hotreloading with webpack based on the tutorial on the NestJs site.
When running npm run webpack it completes successfully.
When running npm run start I am presented with the following error

SyntaxError: Unexpected token {
    at Module._compile (internal/modules/cjs/loader.js:703:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)

Input Code

ormconfig.json

{
  "type": "mysql",
  "host": "127.0.0.1",
  "port": 3306,
  "username": "root",
  "password": "root",
  "database": "test",
  "entities": ["src/**/*.entity{.ts,.js}"],
  "synchronize": false,
  "logging": true,
  "cache": true
}

pacakge.json (scripts)

{
"build": "tsc -p tsconfig.build.json",
"format": "prettier --write \"src//.ts\" \"test//.ts\"",
"startOld": "ts-node -r tsconfig-paths/register src/main.ts",
"start": "node dist/server",
"webpack": "webpack --config webpack.config.js",
"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"
}

Expected behavior

Running npm run webpack and then npm run start should not result in errors

Possible Solution

I searched the internet and could not find anyone with the same issue, but I did find something similar. Someone suggested to change the ormconfig.json file and replace
"entities": ["src/**/*.entity{.ts,.js}"]
with
"entities": ["dist/**/*.entity{.ts,.js}"]
but then I was . presented with a new error stating that typeorm could not find the entities

Environment

Nest version: 5

For Tooling issues:

  • Node version: 12
  • Platform: Mac
needs triage

Most helpful comment

Changing

"entities": ["./src/**/*.entity.ts", "./dist/**/*.entity.js"]

to

"entities": ["dist/**/*.entity{.ts,.js}"]

fixes this issue.

All 11 comments

I get the same (compilation?) error. It also happens when the main.js from the ./dist folder is started. This error is located on a .entity.ts file, on the 'typeorm' import.
As for me I declared the typeorm config in a .env file.

Have you tried to import the entities directly from their file and write "entities": [YourEntity] ?

@victorbrun Thanks for the tip.
Unfortunately any direct reference to the entity files result in a new (better?) error

[Nest] 37152   - 2019-06-14 21:33   [ExceptionHandler] No repository for "User" was found. Looks like this entity is not registered in current "default" connection? +1ms
RepositoryNotFoundError: No repository for "User" was found. Looks like this entity is not registered in current "default" connection?
SyntaxError: Unexpected token {

This error usually means that your compiled app is trying to load original .ts files (before transpilation). Put entities directly into entities: [] array or fix your entities path (so it loads only .js files). With webpack, I'd rather suggest moving with the first option (explicit entities array)

Changing

"entities": ["./src/**/*.entity.ts", "./dist/**/*.entity.js"]

to

"entities": ["dist/**/*.entity{.ts,.js}"]

fixes this issue.

Changing

"entities": ["./src/**/*.entity.ts", "./dist/**/*.entity.js"]

to

"entities": ["dist/**/*.entity{.ts,.js}"]

fixes this issue.

Thanks! That fixes the issue

I was facing the same error, using @appliaison approach solved my issue =) THANKS!

The solution @appliaison suggested worked for me, I think the docs should be updated.

Only way, wich works all cases (compiled, debug, typescript, typescript debug, tests) was import entities manually, one by one and put as array in config.

That's what I would suggest too. We updated the docs with manually imported entities.

Changing

"entities": ["./src/**/*.entity.ts", "./dist/**/*.entity.js"]

to

"entities": ["dist/**/*.entity{.ts,.js}"]

fixes this issue.

Thank you so much ! It worked for me but I don't understand why is the path related to dist/ folder ?

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings