question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

migration:generate throws a SyntaxError with clean project template

See original GitHub issue

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.20 (or put your version here)

typeorm migration:generate throws a SyntaxError when you run it on a brand new project generated by typeorm init.

Steps to reproduce or a small repository showing the problem:

I set up a MySQL database (version 5.6) in a Docker container so that it was accessible via the default configuration in ormconfig.json. Then I did the following:

$ npx typeorm init
$ npm install
$ npx typeorm migration:generate -n User

This was the result:

Error during migration generation:
…/src/entity/User.ts:1
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:849:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Function.PlatformTools.load (…/node_modules/typeorm/platform/PlatformTools.js:109:28)
    at /media/evan.sheehan/workshop/tmp/typeorm_template/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:39:69
    at Array.map (<anonymous>)
    at Object.importClassesFromDirectories (…/node_modules/typeorm/util/DirectoryExportedClassesLoader.js:39:10)

I would expect that, having changed nothing in the project template, I should be able to generate a migration for the User entity.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:14
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

29reactions
msheakoskicommented, Nov 22, 2019

@darthmall That is the recommended way according to https://typeorm.io/#/migrations/running-and-reverting-migrations

I like to add a script to my package.json:

"scripts": {
  "typeorm": "TS_NODE_PROJECT=tsconfig.ts-node.json TS_NODE_TRANSPILE_ONLY=true ts-node ./node_modules/typeorm/cli.js"
}

tsconfig.ts-node.json for reference:

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

and from that point, I run the typeorm cli using yarn typeorm migration:generate -n User

20reactions
TheBenjicommented, Dec 19, 2019

I had the same issue and after looking through it a bit it seems you have to change the ormconfig.json and change

   "entities": [
      "src/entity/**/*.ts"
   ],
   "migrations": [
      "src/migration/**/*.ts"
   ],
   "subscribers": [
      "src/subscriber/**/*.ts"
   ],

to

   "entities": [
      "build/entity/**/*.js"
   ],
   "migrations": [
      "build/migration/**/*.js"
   ],
   "subscribers": [
      "build/subscriber/**/*.js"
   ],

Of course then running npm run start might not work as expected and you’ll have to change that from ts-node to just use node and have a build step before and that seems to work.

Not sure if that’s working as intended or is a bug…

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve SyntaxError on autogenerated manage.py?
I've created a "mysite" dummy project (my very first one) and try to test it without altering it. django-admin startproject mysite cd mysite...
Read more >
ArcGIS Pro - Create Project Template - Esri Community
I am trying to create a project template with all my usual connections, folders, ... ERROR 000989: Python syntax error: within script tool ......
Read more >
TypeORMのMigrationでCannot use import statement outside ...
node_modules/typeorm/cli.js migration:generate -n 'User' ... migration:generate throws a SyntaxError with clean project template #5087 ...
Read more >
Broke site with Translation in twig templates [#3039548] - Drupal
Steps Clean install of Lightning 3.2.6 using composer System info: ... PHP Exception Twig\Error\SyntaxError: "An exception has been thrown ...
Read more >
Ardalis.CleanArchitecture.Template 7.0.1 - NuGet
You should be all set. Clean Architecture Project Template step 2. Note that the template is generally only updated with major updates to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found