Nest: Mongo TypeORM example don't work in production

Created on 24 Feb 2018  路  6Comments  路  Source: nestjs/nest

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


If you test to start Mongo TypeORM example, with start:prod command the console output this error :

[ExceptionHandler] Unexpected token import
/Users/robin/nestTest/examples/13-mongo-typeorm/src/photo/photo.entity.ts:1
(function (exports, require, module, __filename, __dirname) { import { Entity, Column, ObjectIdColumn, ObjectID } from 'typeorm';
                                                              ^^^^^^

I've try to find a solution on the internet but without success :/

Minimal reproduction of the problem with instructions


Pull https://github.com/nestjs/nest/tree/master/examples/13-mongo-typeorm
And run this commands :
npm install
npm run start:prod

Environment


Nest version: 4.5.0

For Tooling issues:

  • Node version: v9.5.0
  • Platform: Mac
question 馃檶

Most helpful comment

@fwoelffel @kamilmysliwiec exactly, but you can't use the same ormconfig.json for developement and prodcution. So I created a config file for my environments and load it using the process.env.NODE_ENVvar.

src/config.env.ts :

const config = {
 development: {
        cors: true,
        port: 8001,
        prefixApi: 'api',
        ormtype: {
            type: 'mongodb',
            host: 'localhost',
            port: 27017,
            database: 'my-database',
            username: 'user',
            password: 'password',
            authSource: 'admin',
            synchronize: true,                       
            entities: ['src/**/**.entity{.ts,.js}'],
        },
    },
    production: {
        cors: false,
        port: 8001,
        ormtype: {
            type: 'mongodb',
            host: 'localhost',
            port: '27017',
            username: 'user',
            password: 'password',
            database: 'my-database',
            authSource: 'admin',
            synchronize: false,                       // <-- recommended by typeorm docs 
            entities: ['dist/**/**.entity{.ts,.js}'], // <-- changed src directory to dist
        },
    },
};

const envConfig = config[process.env.NODE_ENV || 'development'];

export default envConfig;

src/app.module.ts :

...
import envConfig from './config.env';

const {ormtype: ormConfig} = envConfig;

@Module({
    imports: [
        TypeOrmModule.forRoot(ormConfig),
       ...
    ],
})
export class ApplicationModule implements NestModule {}

All 6 comments

@RobinPrette did u try with "moduleResolution": "node", in tsconfig.json?

@chanlito I've just try with it but that doesn't work

Note that TypeORM is looking for entities using the following path:

13-mongo-typeorm/src/photo/photo.entity.ts

Production build is inside dist directory, not src. In order to fix that you have to modify ormconfig.json.

If you are using the path to your entity files when initializing the TypeORM connection, make sure you give the path to the dist folder, as shown here https://docs.nestjs.com/techniques/sql

@fwoelffel @kamilmysliwiec exactly, but you can't use the same ormconfig.json for developement and prodcution. So I created a config file for my environments and load it using the process.env.NODE_ENVvar.

src/config.env.ts :

const config = {
 development: {
        cors: true,
        port: 8001,
        prefixApi: 'api',
        ormtype: {
            type: 'mongodb',
            host: 'localhost',
            port: 27017,
            database: 'my-database',
            username: 'user',
            password: 'password',
            authSource: 'admin',
            synchronize: true,                       
            entities: ['src/**/**.entity{.ts,.js}'],
        },
    },
    production: {
        cors: false,
        port: 8001,
        ormtype: {
            type: 'mongodb',
            host: 'localhost',
            port: '27017',
            username: 'user',
            password: 'password',
            database: 'my-database',
            authSource: 'admin',
            synchronize: false,                       // <-- recommended by typeorm docs 
            entities: ['dist/**/**.entity{.ts,.js}'], // <-- changed src directory to dist
        },
    },
};

const envConfig = config[process.env.NODE_ENV || 'development'];

export default envConfig;

src/app.module.ts :

...
import envConfig from './config.env';

const {ormtype: ormConfig} = envConfig;

@Module({
    imports: [
        TypeOrmModule.forRoot(ormConfig),
       ...
    ],
})
export class ApplicationModule implements NestModule {}

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?
5 / 5 - 1 ratings

Related issues

mishelashala picture mishelashala  路  3Comments

breitsmiley picture breitsmiley  路  3Comments

artaommahe picture artaommahe  路  3Comments

VRspace4 picture VRspace4  路  3Comments

FranciZ picture FranciZ  路  3Comments