Nest: Cannot use import statement outside a module

Created on 30 Oct 2020  路  7Comments  路  Source: nestjs/nest

Bug Report

Current behavior

yarn start or yarn start:dev

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

SyntaxError: Cannot use import statement outside a module

Expected behavior

Build without errors

Environment

Nest version: 7.5.1

For Tooling issues:

  • Node version: 14.12.5
  • Platform: MacOS

Other:

package.json

{
  "private": true,
  "license": "UNLICENSED",
  "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",
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
    "typeorm:migration": "typeorm migration:create -d src/database/migrations/ -n",
    "typeorm:create-entity": "typeorm entity:create -d src/entities -n"
  },
  "dependencies": {
    "@nestjs/common": "^7.0.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/platform-express": "^7.0.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.5.4"
  },
  "devDependencies": {
    "@nestjs/cli": "^7.0.0",
    "@nestjs/config": "^0.5.0",
    "@nestjs/schematics": "^7.0.0",
    "@nestjs/testing": "^7.0.0",
    "@types/express": "^4.17.3",
    "@types/jest": "26.0.10",
    "@types/node": "^13.9.1",
    "@types/supertest": "^2.0.8",
    "@typescript-eslint/eslint-plugin": "3.9.1",
    "@typescript-eslint/parser": "3.9.1",
    "eslint": "7.7.0",
    "eslint-config-prettier": "^6.10.0",
    "eslint-plugin-import": "^2.20.1",
    "jest": "26.4.2",
    "pg": "^8.4.1",
    "prettier": "^1.19.1",
    "supertest": "^4.0.2",
    "ts-jest": "26.2.0",
    "ts-loader": "^6.2.1",
    "ts-node": "9.0.0",
    "tsconfig-paths": "^3.9.0",
    "typeorm": "^0.2.28",
    "typescript": "^3.7.4"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

ormconfig.ts

import { ConnectionOptions } from 'typeorm';

export const config: ConnectionOptions = {
  type: 'postgres',
  host: process.env.DB_HOST,
  port: parseFloat(process.env.DB_PORT),
  username: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_DATABASE,
  entities: [__dirname + '/../**/*.entity{.ts,.js}'],
  migrations: ['dist/database/migrations/*.js'],
  synchronize: true,
};
needs triage

All 7 comments

Please use our Discord for support. We are using GitHub to track Bug Reports, Feature Requests, and Code Regressions.

So I won't get an answer to my question here?

You can search through our past issues too, this is something that seems to be common, and has already been answered. There's also this StackOverflow answer which has the exact same information.

I have already tried these solutions.
But they don't work in my case.
The problem still exists.

Please use our Discord for support. We are using GitHub to track Bug Reports, Feature Requests, and Code Regressions.

Then please follow our community standards and let's discuss this on Discord.

Nice help.
"Thanks".

Please, use Discord for such questions. This isn't a bug but rather an issue that sits within your codebase (very likely related to an invalid glob pattern specified in the entities array in the ORM configuration).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

VRspace4 picture VRspace4  路  3Comments

cdiaz picture cdiaz  路  3Comments

anyx picture anyx  路  3Comments

cojack picture cojack  路  3Comments

tronginc picture tronginc  路  3Comments