47
votes

Started new project with 'nest new' command. Works fine until I add entity file to it.

Got following error:

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

^^^^^^

SyntaxError: Cannot use import statement outside a module

What do I miss?

Adding Entity to Module:

import { Module } from '@nestjs/common';
import { BooksController } from './books.controller';
import { BooksService } from './books.service';
import { BookEntity } from './book.entity';
import { TypeOrmModule } from '@nestjs/typeorm';

@Module({
  imports: [TypeOrmModule.forFeature([BookEntity])],
  controllers: [BooksController],
  providers: [BooksService],
})
export class BooksModule {}

app.module.ts:

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Connection } from 'typeorm';
import { BooksModule } from './books/books.module';

@Module({
  imports: [TypeOrmModule.forRoot()],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
14
import { Module } from '@nestjs/common';Preston
@Preston care to elaborate on what you mean? Do you have to create a module for commonly shared files?Joshua de Leon
Are you getting the error from your linter or from a compilation? Where do you have this new file? Is it in your src directory? If you're using TypeORM, can you show your TypeOrmModule import in the AppModule's imports array? There may be something wrong with the configuration we can't seeJay McDoniel
updated post with entity import infoAnton

14 Answers

100
votes

My assumption is that you have a TypeormModule configuration with an entities property that looks like this:

entities: ['src/**/*.entity.{ts,js}']

or like

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

The error you are getting is because you are attempting to import a ts file in a js context. So long as you aren't using webpack you can use this instead so that you get the correct files

entities: [join(__dirname, '**', '*.entity.{ts,js}')]

where join is imported from the path module. Now __dirname will resolve to src or dist and then find the expected ts or js file respectively. let me know if there is still an issue going on.

EDIT 1/10/2020

The above assumes the configuration is done is a javascript compatible file (.js or in the TypeormModule.forRoot() passed parameters). If you are using an ormconfig.json instead, you should use

entities: ["dist/**/*.entity.js"]

so that you are using the compiled js files and have no chance to use the ts files in your code.

27
votes

In the TypeORM documentation, i found a specific section for Typescript.

This section says:

Install ts-node globally:

npm install -g ts-node

Add typeorm command under scripts section in package.json

"scripts" {
    ...
    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js"    
}

Then you may run the command like this:

npm run typeorm migration:run

If you need to pass parameter with dash to npm script, you will need to add them after --. For example, if you need to generate, the command is like this:

npm run typeorm migration:generate -- -n migrationNameHere

This works with my file config:

{
    "type": "postgres",
    "host": "yourhost",
    "port": 5423,
    "username": "username",
    "password": "password",
    "database": "your_db",
    "synchronize": true,
    "entities": [
        "src/modules/**/*.entity.{ts,js}"
    ],
    "migrations": [
        "src/migrations/**/*.{ts,js}"
    ],
    "cli": {
        "entitiesDir": "src/modules",
        "migrationsDir": "src/migrations"
    }
}

Then you can run the generate command.

12
votes

As Jay McDoniel explained in his answer, the problem seems to be the pattern matching of entity files in ormconfig.json file: Probably a typescript file (module) is imported from a javascript file (presumably a previously transpiled typescript file).

It should be sufficient to remove an existing ts glob pattern in the ormconfig.json, so that TypeORM will only load javascript files. The path to the entity files should be relative to the working directory where node is executed.

   "entities"   : [
      "dist/entity/**/*.js"
   ],
   "migrations" : [
      "dist/migration/**/*.js"
   ],
   "subscribers": [
      "dist/subscriber/**/*.js"
   ],
7
votes

I changed in tsconfig.json file next:

"module": "es6"

To:

"module": "commonjs",

It helps me

4
votes

This is how I've manage to fix it. With a single configuration file I can run the migrations on application boostrap or using TypeOrm's CLI.

src/config/ormconfig.ts

import parseBoolean from '@eturino/ts-parse-boolean';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import * as dotenv from 'dotenv';
import { join } from 'path';

dotenv.config();

export = [
  {
    //name: 'default',
    type: 'mssql',
    host: process.env.DEFAULT_DB_HOST,
    username: process.env.DEFAULT_DB_USERNAME,
    password: process.env.DEFAULT_DB_PASSWORD,
    database: process.env.DEFAULT_DB_NAME,
    options: {
      instanceName: process.env.DEFAULT_DB_INSTANCE,
      enableArithAbort: false,
    },
    logging: parseBoolean(process.env.DEFAULT_DB_LOGGING),
    dropSchema: false,
    synchronize: false,
    migrationsRun: parseBoolean(process.env.DEFAULT_DB_RUN_MIGRATIONS),
    migrations: [join(__dirname, '..', 'model/migration/*.{ts,js}')],
    cli: {
      migrationsDir: 'src/model/migration',
    },
    entities: [
      join(__dirname, '..', 'model/entity/default/**/*.entity.{ts,js}'),
    ],
  } as TypeOrmModuleOptions,
  {
    name: 'other',
    type: 'mssql',
    host: process.env.OTHER_DB_HOST,
    username: process.env.OTHER_DB_USERNAME,
    password: process.env.OTHER_DB_PASSWORD,
    database: process.env.OTHER_DB_NAME,
    options: {
      instanceName: process.env.OTHER_DB_INSTANCE,
      enableArithAbort: false,
    },
    logging: parseBoolean(process.env.OTHER_DB_LOGGING),
    dropSchema: false,
    synchronize: false,
    migrationsRun: false,
    entities: [],
  } as TypeOrmModuleOptions,
];

src/app.module.ts

import configuration from '@config/configuration';
import validationSchema from '@config/validation';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { LoggerService } from '@shared/logger/logger.service';
import { UsersModule } from '@user/user.module';
import { AppController } from './app.controller';
import ormconfig = require('./config/ormconfig'); //path mapping doesn't work here

@Module({
  imports: [
    ConfigModule.forRoot({
      cache: true,
      isGlobal: true,
      validationSchema: validationSchema,
      load: [configuration],
    }),
    TypeOrmModule.forRoot(ormconfig[0]), //default
    TypeOrmModule.forRoot(ormconfig[1]), //other db
    LoggerService,
    UsersModule,
  ],
  controllers: [AppController],
})
export class AppModule {}

package.json

  "scripts": {
    ...
    "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config ./src/config/ormconfig.ts",
    "typeorm:migration:generate": "npm run typeorm -- migration:generate -n",
    "typeorm:migration:run": "npm run typeorm -- migration:run"
  },

Project structure

src/
├── app.controller.ts
├── app.module.ts
├── config
│   ├── configuration.ts
│   ├── ormconfig.ts
│   └── validation.ts
├── main.ts
├── model
│   ├── entity
│   ├── migration
│   └── repository
├── route
│   └── user
└── shared
    └── logger
3
votes

Defining the entities property in ormconfig.json as mentioned in the official documentation resolved this issue for me.

// This is your ormconfig.json file

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

Actually, typeorm was designed to work with javascript by default.

To run the migrations with typescript, you must tell typeorm to do it.

Just put in your package.json, in the scripts part this line below:

"typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"

and then, try to migrate again:

yarn typeorm migration:run
1
votes

You need to have a something.module.ts for every section of your app. It works like Angular. This is setup with GraphQL resolvers and service. REST is a bit different with a controller. Each module will probably have an entity and if GraphQL, projects.schema.graphql.

projects.module.ts

import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ProjectsService } from './projects.service';
import { Projects } from './projects.entity';

import { ProjectsResolvers } from './projects.resolvers';

@Module({
  imports: [
    TypeOrmModule.forFeature([Projects])],
  providers: [
    ProjectsService,
    ProjectsResolvers
  ],

})

export class ProjectsModule {}
1
votes

This worked for me - no changes needed to your ormconfig.js. Run from your root directory where the node_modules are:

ts-node ./node_modules/typeorm/cli.js  migration:generate -n <MirgrationName> -c <ConnectionType>

Example:

ts-node ./node_modules/typeorm/cli.js  migration:create -n AuthorHasMultipleBooks -c development 
1
votes

In line with other people's comments - it does in fact seem silly to have to depend on generated code for this to work. I do not take credit for this solution as it's someone else's repository, but it does in fact allow full Typescript only migrations. It relies on the .env file Typeorm values instead of ormconfig.json although I'm sure it could be translated. I found it instrumental in helping me remove the dependency on .js files.

Here is the repo: https://github.com/mthomps4/next-now-test/tree/next-typeorm-example

Explanation as to how it's working:

Aside from your usual .env or ormconfig.json file with the proper localhost db connection in it, you also need to specify the following properly in ormconfig.json or .env file

TYPEORM_ENTITIES="entities/*.ts"
TYPEORM_MIGRATIONS="migrations/*.ts"
TYPEORM_ENTITIES_DIR="entities"
TYPEORM_MIGRATIONS_DIR="migrations"

Notice the entities and migrations globs only have *.ts. The other very important piece is how your npm scripts are setup to run with ts-node.

You need an extended tsconfig that has the following in it somewhere:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs"
  }
}

This is what allows ts-node to "pick up" the .ts files properly while generating a migration.

This npm script (the DOTENV part is only if using .env files instead of ormconfig.json) specifies to use that tsconfig.json

 "local": "DOTENV_CONFIG_PATH=./.env ts-node -P ./tsconfig.yarn.json -r dotenv/config"

Which is leveraged as a "pre-cursor" script to this:

"typeorm:local": "yarn local ./node_modules/typeorm/cli.js"

I'm not 100% sure all of that is necessary (you may could do it all inline) but it works for me. Basically this says "invoke the typrorm cli in the context of ts-node with a specific .env file and a specific tsconfig." You may be able to skip those configurations in some cases.

Lastly, this script now works:

"g:migration": "yarn typeorm:local migration:generate -n"

So by running:

npm run g:migration -- User

You will get your automatically generated migration file based on your current changed entities!

So 3 nested npm scripts later, we have a very specific way to run the "generate" migration conmmand with all the proper configuration to use only TS files. Yay - no wonder some people still rail against typescript but thankfully this does work and the example repo above has it all preconfigured if you want to try it out to see how it "just works".

0
votes

I was using Node.js with Typescript and TypeORM when I faced this issue. Configuring in ormconfig.json file worked for me.

entities: ['dist/**/*.entity.js']

My full code of ormconfig.json file:

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "xxxxxxxx",
  "password": "xxxxxxxx",
  "database": "typescript_orm",
  "synchronize": true,
  "logging": false,
  "migrationTableName": "migrations",
  "entities": [
    "dist/**/*.entity.js"
  ],
  "migrations": [
    "src/migration/**/*.{ts, js}"
  ],
  "suscribers": [
    "src/suscriber/**/*.{ts, js}"
  ],
  "cli": {
    "entitiesDir": "src/model",
    "migrationDir": "src/migration",
    "suscribersDir": "src/suscriber"
  }
}
0
votes

Configuration to support migrations:

// FILE: src/config/ormconfig.ts

const connectionOptions: ConnectionOptions = {
  
  // Other configs here

  // My ormconfig isn't in root folder
  entities: [`${__dirname}/../**/*.entity.{ts,js}`],
  synchronize: false,
  dropSchema: false,
  migrationsRun: false,
  migrations: [getMigrationDirectory()],
  cli: {
    migrationsDir: 'src/migrations',
  }
}

function getMigrationDirectory() {
    const directory = process.env.NODE_ENV === 'migration' ? 'src' : `${__dirname}`;
    return `${directory}/migrations/**/*{.ts,.js}`;
}

export = connectionOptions;
// FILE package.json

{
  // Other configs here

  "scripts": {
    "typeorm": "NODE_ENV=migration ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/config/database.ts",
    "typeorm:migrate": "npm run typeorm migration:generate -- -n",
    "typeorm:run": "npm run typeorm migration:run",
    "typeorm:revert": "npm run typeorm migration:revert"
  }
}
0
votes

If you are writing in typescript and use tsc to create a dist folder with translated js files in it, then you probably have my issue and it will get fixed here.

As it is mentioned here in the docs if you use nodemon server.js, then you will hit the entities from js perspective and it will not recognize import as it is ts and es6 related. However if you want to import entities from ts files, you should run ts-node server.ts!

Personally I believe the former node server.js is a safer one to do as it is closer to the real case application.

!!! HOWEVER !!! Be very careful as you have to delete the dist folder and rebuild it if you change an entity's name, otherwise it will throw an error or work unexpectedly. The error happens because the tsc will try to translate the changed and created ts files and leave the deleted files so it can run faster!

I hope it helped as it will definitely help me in the future as I am almost certain I will forget about it again!

0
votes

I think a better solution, than the accepted one, is to create a alias in your shell of choice, that uses ts-node inside node_modules.

Note: I'm doing this in bash, with OhMyZsh, so your configuration might be totally different.

1: Open shell configuration

Open shell configuration1

nano ~/.zshrc

2: Find the place where other aliases are defined and add a new alias

alias typeorm="ts-node ./node_modules/typeorm/cli.js"

3: Close and save

Press CTRL + X to request nano to exit and press Y to confirm to save the configuration.

4: Apply the new configuration

. ~/.zshrc

5: Close terminal and open it again

You can now go to your project root and type "typeorm" which will use ts-node in conjunction with the typeorm-cli from your node_modules.