New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webpack start:hmr] SyntaxError: Unexpected token import on TypeORM entity files #1018

Closed
fedme opened this Issue Aug 29, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@fedme
Copy link

fedme commented Aug 29, 2018

I'm submitting a...


[ ] 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

When running npm run start:hmr (note that npm run webpack is running) on a project created with the CLI and containing a TypeORM entity file, I get the following error:

[Nest] 10906   - 2018-8-29 16:41:14   [TypeOrmModule] Unable to connect to the database. Retrying (6)... +3011ms
/mnt/c/********/src/records/record.entity.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, Column, PrimaryGeneratedColumn, VersionColumn, CreateDateColumn,
UpdateDateColumn } from 'typeorm';
                                                              ^^^^^^

SyntaxError: Unexpected token import

Expected behavior

I would expect no errors. It seems like the entity file does not get compiled to JavaScript.

Minimal reproduction of the problem with instructions

  1. Create a new project with the Nest CLI
  2. Add TypeORM to the project
  3. Create a TypeORM entity file
  4. Run npm run webpack
  5. On another terminal run npm run start:hmr
  6. Observe the error

What is the motivation / use case for changing the behavior?

Environment


[System Information]
OS Version     : Linux 4.4
NodeJS Version : v8.11.4
NPM Version    : 6.4.0
[Nest Information]
typeorm version : 5.2.0
common version  : 5.0.0
core version    : 5.0.0

I am new to Nest so please excuse me If I am making a stupid mistake.
I was not able to find details in the documentation about hot reloading on a CLI project. Thanks!

@fedme

This comment has been minimized.

Copy link

fedme commented Aug 30, 2018

I was able to fix this issue by configuring TypeORM inside my app's root module (instead of using a ormconfig.json file) and referencing my entities by class instead of by file as suggested in #711 .

Is this the correct way / is there some other better solution? Thanks.
If there is, I really think the docs should have a section on webpack + typeorm. I would like to help but I am totally new to Nest :)

My root module now:

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RecordsModule } from './records/records.module';
import { Record } from './records/record.entity';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      keepConnectionAlive: true,
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: '***',
      password: '***',
      database: '***',
      synchronize: true, // TODO: Remove in production!
      entities: [ // Passing entities as class references so that webpack doesn't break
        Record
      ],
      migrations: [__dirname + '/migration/**/*.ts'],
      subscribers: [__dirname + '/subscriber/**/*.ts'],
    }),
    RecordsModule,
  ],
  controllers: [
    AppController,
  ],
  providers: [
    AppService,
  ],
})
export class AppModule {}
@whtiehack

This comment has been minimized.

Copy link

whtiehack commented Aug 30, 2018

try change tsconfig.json -> compilerOptions -> target to es6 .

@fedme

This comment has been minimized.

Copy link

fedme commented Aug 30, 2018

@whtiehack thanks but it was already set to es6.

@kamilmysliwiec

This comment has been minimized.

Copy link
Member

kamilmysliwiec commented Aug 31, 2018

The entities are loaded on the fly by the TypeOrm, and therefore, webpack doesn't that these paths should be treated differently than just plain literal strings. Your solution is the best available one so far.

@zhyupe

This comment has been minimized.

Copy link

zhyupe commented Oct 14, 2018

Maybe a better workaround:

Write a loader like this and pass require('path/to/ormconfig.json') to TypeOrmModule.forRoot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment