Nest: SyntaxError: Cannot use import statement outside a module

Created on 12 Mar 2020  路  6Comments  路  Source: nestjs/nest

Bug Report

Current behavior

when running 'npm run start' I get an error as described here: https://stackoverflow.com/questions/59435293/typeorm-entity-in-nestjs-cannot-use-import-statement-outside-a-module

Input Code

repo: https://github.com/Bautista-Baiocchi-lora/LAB1-Server

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

@Entity()
export default class Barrio{

    @PrimaryGeneratedColumn()
    barrio_id: number;

    @Column()
    email: string;

    @Column()
    password: string;

    @Column()
    name: string;

}

Expected behavior

Server should start up without any problems.

Environment

package.json

{
"name": "nest-typescript-starter",
"private": true,
"version": "1.0.0",
"description": "Nest TypeScript starter repository",
"license": "MIT",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"format": "prettier --write \"src//.ts\" \"test//.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}//.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^6.10.14",
"@nestjs/core": "^6.10.14",
"@nestjs/platform-express": "^6.10.14",
"@nestjs/typeorm": "^6.3.4",
"pg": "^7.18.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.0",
"rxjs": "^6.5.4",
"typeorm": "^0.2.24"
},
"devDependencies": {
"@nestjs/cli": "^6.13.2",
"@nestjs/schematics": "^6.8.1",
"@nestjs/testing": "^6.10.14",
"@types/express": "^4.17.2",
"@types/jest": "^24.9.1",
"@types/node": "^13.1.6",
"@types/supertest": "^2.0.8",
"@typescript-eslint/eslint-plugin": "^2.12.0",
"@typescript-eslint/parser": "^2.12.0",
"eslint": "^6.7.2",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-import": "^2.19.1",
"jest": "^24.9.0",
"prettier": "^1.18.2",
"supertest": "^4.0.2",
"ts-jest": "^24.3.0",
"ts-loader": "^6.2.1",
"ts-node": "^8.6.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^3.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"
/.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}

needs triage

Most helpful comment

I'll go ahead and answer this with a rehash of my answer on the StackOverflow question you linked: you are trying to load Typescript files while running JavaScript, which will fail 100% of the time. Your entities array of your TypeOrmModule.forRoot() configuration is looking for js and ts files in the src directory, which should be only where your ts files live. Your js files should exist solely in dist (at least the ones created from compilation), and you shouldn't really look for the ts files in the first place. If you want to be able to load ts or js files, you should use __dirname instead of src. In the end though, as you should be using node to run the server, and so long as you aren't using ts-node for development, you should replace the line completely with dist/**/*.entity.js

All 6 comments

I'll go ahead and answer this with a rehash of my answer on the StackOverflow question you linked: you are trying to load Typescript files while running JavaScript, which will fail 100% of the time. Your entities array of your TypeOrmModule.forRoot() configuration is looking for js and ts files in the src directory, which should be only where your ts files live. Your js files should exist solely in dist (at least the ones created from compilation), and you shouldn't really look for the ts files in the first place. If you want to be able to load ts or js files, you should use __dirname instead of src. In the end though, as you should be using node to run the server, and so long as you aren't using ts-node for development, you should replace the line completely with dist/**/*.entity.js

Thanks for sharing the solution @jmcdo29 :)

I'll go ahead and answer this with a rehash of my answer on the StackOverflow question you linked: you are trying to load Typescript files while running JavaScript, which will fail 100% of the time. Your entities array of your TypeOrmModule.forRoot() configuration is looking for js and ts files in the src directory, which should be only where your ts files live. Your js files should exist solely in dist (at least the ones created from compilation), and you shouldn't really look for the ts files in the first place. If you want to be able to load ts or js files, you should use __dirname instead of src. In the end though, as you should be using node to run the server, and so long as you aren't using ts-node for development, you should replace the line completely with dist/**/*.entity.js

Sorry for bring the problem back up. Your solution worked only AFTER i rebuilt the dist directory. I found that dist had old build files. These were clashing with the new changes.

In case anyone else is wondering about this checkout this answer on stackoverflow https://stackoverflow.com/a/61119284/1072058.

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;

```json
// FILE package.json

{
"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"
}
}

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cojack picture cojack  路  3Comments

tronginc picture tronginc  路  3Comments

menme95 picture menme95  路  3Comments

janckerchen picture janckerchen  路  3Comments

artaommahe picture artaommahe  路  3Comments