typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

TypeORM fails at 'import { getMetadataArgsStorage }', Unexpected token {

Open varogonz95 opened this issue 7 years ago • 5 comments

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:

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

Steps to reproduce or a small repository showing the problem: Hi, I'm building a MVC framework with Express and Typescript. I published my package to npm (which means I transpiled it to .js files). Using my framework, I wanted to add TypeORM. Everything worked Ok when running my project with ts-node, but when transpiling and running it with node it fails.

I managed to print the error output into console:

C:\...\<my project>\node_modules\typeorm\browser\decorator\entity\ChildEntity.js:1
(function (exports, require, module, __filename, __dirname) { import { getMetadataArgsStorage } from "../../";
                                                                     ^

SyntaxError: Unexpected token {
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)

As you can see, it is importing getMetadataArgsStorage from .../browser/decorator path. Is this right? Thanks in advance!

varogonz95 avatar Mar 22 '19 23:03 varogonz95

This error pops in NodeJs when you try to execute JS code containing ES6 (es2015) module imports. NodeJs does not understand that syntax, it expects JS code to use CommonJs syntax to import modules.

From your TypeScript code, you need to:

  • Either transpile into full ES5 code. This will generate CommonJs module imports.
{
    "compilerOptions": {
        ...
        "target": "es5"
    }
}
  • Either transpile into ES6 code, but with CommonJs generated module imports.
{
    "compilerOptions": {
        ...
        "target": "es6",
        "module": "commonjs"
    }
}

nicomouss avatar Mar 23 '19 10:03 nicomouss

Ok, I had my suspicions on that :/ Everything working as expected now. Thank you!

varogonz95 avatar Mar 23 '19 19:03 varogonz95

This error pops in NodeJs when you try to execute JS code containing ES6 (es2015) module imports. NodeJs does not understand that syntax, it expects JS code to use CommonJs syntax to import modules.

From your TypeScript code, you need to:

  • Either transpile into full ES5 code. This will generate CommonJs module imports.
{
    "compilerOptions": {
        ...
        "target": "es5"
    }
}
  • Either transpile into ES6 code, but with CommonJs generated module imports.
{
    "compilerOptions": {
        ...
        "target": "es6",
        "module": "commonjs"
    }
}

I still get the error

canhbk avatar Aug 30 '19 10:08 canhbk

how can you solve this problem?

Erchoc avatar Oct 14 '19 22:10 Erchoc

how can you solve this problem?

If you read the whole thread (or even the comment above yours), you can find the solution. Just change the compiler target

varogonz95 avatar Oct 14 '19 22:10 varogonz95