1
votes

I'm struggling with a weird issue in NestJS + TypeORM.

Pretty much I've created a ManyToMany relation in an entity and, on the owning side of the relation, I've added the @JoinTable() with no parameters.

After running nest build, the entity.js file adds a new import const browser_1 = require("typeorm/browser");, which, I found out, is used in declaring the JoinTable option in the compiled JS file => browser_1.JoinTable().

The issue is that, when using typeorm to generate a new migration file, I keep getting the following error:

import { __awaiter, __generator } from "tslib";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.require.extensions.<computed> [as .js] (C:\XXX\XXX\dev\XXX\pistis-api\node_modules\ts-node\src\index.ts:1045:43)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (C:\XXX\XXX\dev\XXX\pistis-api\dist\modules\collaborator\entity\collaborator.entity.js:17:19)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)

Tracking down the issue, it seems to be related to this browser import that is being imported from ./node_modules/typeorm/browser/index.js

I'm using .env to configure typeorm, with the following:

TYPEORM_CONNECTION=postgres
TYPEORM_HOST=localhost
TYPEORM_USERNAME=xxxxx
TYPEORM_PASSWORD=xxxxx
TYPEORM_DATABASE=xxxx
TYPEORM_PORT=5432
TYPEORM_SYNCHRONIZE=false
TYPEORM_LOGGING=true
TYPEORM_DROP_SCHEMA=false
TYPEORM_MIGRATIONS_RUN=true
TYPEORM_ENTITIES=dist/modules/**/entity/*.js
TYPEORM_ENTITIES_DIR=src/modules/**/entity
TYPEORM_MIGRATIONS=dist/migrations/*.js
TYPEORM_MIGRATIONS_DIR=src/migrations
TYPEORM_MIGRATIONS_TABLE_NAME='orm_migrations'

This configuration has worked until I've introduced the ManyToMany relation and the JoinTable. As far as .tsconfig goes, I have:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": [
      "dom",
      "es6",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "declaration": true,
    "removeComments": true,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": false,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  },
  "include": [
    "src"
  ]
}

Any help would be appreciated.

Thanks in advance!

1

1 Answers

4
votes

So, after a month of not touching this project, I decided to restart it and take a fresh look into it. Pretty much came to find out it was a newbie issue related to the way I'm importing the JoinTable option. I pretty much trusted the IDE to do the import and that was my mistake. So, to sum it up:

I had: import {JoinTable} from 'typeorm/browser';

When I should have: import {JoinTable} from 'typeorm';

For future reference, these import errors might also originate from simple mistakes like these and not the complex ones we find on Stackoverflow telling us to check tsconfig.json and the likes.

Thanks :)