Otto Cheley
Otto Cheley

Reputation: 572

unexpected token import nodejs, typescript

I am using typescript, gruntjs, typeorm, typeorm-model-generator in my project for developing a Web API, when I try to start my app I receive an unexpected token import error, but that is because is reading where I got my typescript files when it has to read the dist directory which is where all my js files are, and this error only happens if I add the code required to establish the connection to my database.

app.ts

export class Server {

 public app: express.Application; 

 public static bootstrap(): Server {
     return new Server();
 }

 constructor() {
    this.app = express();

    this.config();
    this.api();
 }

  private config(): void {
    this.app.use(express.static(path.join(__dirname, "public")));

    this.app.use(logger('dev'));
    this.app.use(bodyParser.json());
    this.app.use(bodyParser.urlencoded({
        extended: true
    }));

    this.app.use(function (error: any,
        request: express.Request, response: express.Response,
        next: express.NextFunction) {
            console.log(error);
            error.status = 404;
            response.json(error);
        });
  }

  private api(): void {
   // code that causes error
   // the output I recieve says
   // ./src/entity/myEntity.ts:1 unexpected token import
   // but if I remove it everything works fine
   typeorm.createConnection().then(async connection => {
        console.log("Conexion establecida");
    }).catch(error => {
        console.log(error)
    });
  }

}

This is my tsconfig.json

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

What am I doing wrong? Should I kill myself already.

Upvotes: 3

Views: 2091

Answers (1)

tootall414
tootall414

Reputation: 1306

I recently ran into this issue. The problem is likely in your ormconfig.json. The generated ormconfig.json points to the entities in your src folder as the ones to call, instead of the entity js files in your dist or build folder. Checkout the question I also asked: SyntaxError: Unexpected token import typeORM entity

Upvotes: 3

Related Questions