[ ] 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.
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 :/
Pull https://github.com/nestjs/nest/tree/master/examples/13-mongo-typeorm
And run this commands :
npm install
npm run start:prod
Nest version: 4.5.0
For Tooling issues:
- Node version: v9.5.0
- Platform: Mac
@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_ENV
var.
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.
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 theprocess.env.NODE_ENV
var.src/config.env.ts
:src/app.module.ts
: