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.

Mongo TypeORM example don't work in production

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
RobinPrettecommented, Feb 25, 2018

@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 {}
7reactions
kamilmysliwieccommented, Feb 24, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeORM official relation example not working - Stack Overflow
TypeOrm doesn't seem to make it easy to handle relations automatically: ... are only supported in relational databases, they are not supported by...
Read more >
SQL (TypeORM) | NestJS - A progressive Node.js framework
Warning In this article, you'll learn how to create a DatabaseModule based on the TypeORM package from scratch using custom providers mechanism.
Read more >
DataSource API - typeorm - GitBook
mongoManager - MongoEntityManager used to work with entities for mongodb data source. For more information about MongoEntityManager see MongoDB documentation.
Read more >
entity metadata not found typeorm - You.com | The AI Search ...
I am using NestJS with PostgreSQL and I had the same issue. The problem was by me is that I forgot to import...
Read more >
db.collection.findOneAndUpdate() — MongoDB Manual
This is not the documentation for database commands or language-specific ... For example, if the update statement includes the identifier x (possibly ...
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