5

Just started working with typescript. Unfortunately when I try to build for production it fails.

Firstly I run

tsc

This passes without any error, but when I try to run the build file I get import errors

node build/index.js

The error I get is below:

[0] (function (exports, require, module, __filename, __dirname) { import {
[0]                                                               ^^^^^^
[0]
[0] SyntaxError: Unexpected token import
[0]     at createScript (vm.js:80:10)
[0]     at Object.runInThisContext (vm.js:139:10)

Below is my tsconfig

{
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ],
   "compilerOptions": {
        "lib": [
            "es5",
            "es6",
        ],
        "pretty": true,
        "target": "es5",
        "module": "commonjs",
        "outDir": "./build",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "sourceMap": true
   }
}

I am using node v8.9.3

3
  • make target to es6 Commented Mar 10, 2018 at 14:08
  • @gokcand it is the same issue when I set target to es6 Commented Mar 10, 2018 at 14:38
  • I have just realised that the issue is with typeorm Commented Mar 10, 2018 at 16:49

2 Answers 2

11

If you use TypeORM there can be a problem with your ormconfig. Your configuration file probably contains path like src/entities/*.ts in the entity section. So it causes requiring *.ts files from your src folder, not from dist folder.

Sign up to request clarification or add additional context in comments.

Comments

5

When working with NodeJs, your tsconfig.json should look like this:

{
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ],
   "compilerOptions": {
        "lib": ["es6"],        // No need for "es5" if you have "es6"
        "types": ["node"],      // When you code for nodejs
        "target": "es6",       // NodeJs v8.9.3 supports most of the es6 features
        "pretty": true,
        "module": "commonjs",
        "outDir": "./build",
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "sourceMap": true
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.