简体   繁体   English

语法错误:意外令牌{在执行TS应用时

[英]Syntax error: Unexpected token { on executing a TS app

I have the following code in TypeScript: 我在TypeScript中有以下代码:

import { Document, Schema, Model, model } from "mongoose";
import { IUser } from "../interfaces/IUser";

export interface IUserModel extends IUser, Document {
}

var UserSchema: Schema = new Schema({
    name: String,
    username: String,
    password: String,
    email: String
});

export const User: Model<IUserModel> = model<IUserModel>("User", UserSchema);

Then I use it in a controller. 然后我在控制器中使用它。 When I compile my TS app (tsc app.ts) it compiles fine. 当我编译TS应用程序(tsc app.ts)时,它可以正常编译。 Then when I type "node app.js" in the terminal I get this error: 然后,当我在终端中键入“ node app.js”时,出现此错误:

..\BlocG\models\user.ts:1
(function (exports, require, module, __filename, __dirname) { import { Document, Schema, Model, model } from "mongoose";
                                                                     ^

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:656:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (E:\Business\Cevian\CevianPrep\BlocG\data\db.ts:26:1)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)

What makes it even stranger is that I have such imports in the controller: 使它变得更加奇怪的是,我在控制器中有这样的导入:

import { Router, Request, Response } from 'express';
import { IUserModel } from '../models';

...and it compiles with no problem. ...并且可以毫无问题地进行编译。 I used to execute the logic without any problems. 我曾经执行逻辑时没有任何问题。

Please note that the error is not pointed at the import keyword but rather the curly bracket. 请注意,错误不是针对import关键字,而是大括号。

Please help me with this issue! 请帮我解决这个问题! Thanks in advance! 提前致谢!

Then when I type "node app.js" in the terminal I get this error: 然后,当我在终端中键入“ node app.js”时,出现此错误:

Change your tsconfig module option to be something that will work both in node (natively) and browser (using eg webpack): 将您的tsconfig module选项更改为在节点(本机)和浏览器(例如使用webpack)中都可以使用的选项:

"module": "commonjs"

As it turns out, the basic problem was that I was compiling like this: 事实证明,基本问题是我正在这样编译:

tsc app

Instead of just writing 不只是写作

tsc

This caused my compilation types to mix with the js ones in the same folder. 这导致我的编译类型与同一文件夹中的js混合在一起。

Also, I had imports directly from [name].ts files, where I should've used file names only. 另外,我直接从[name] .ts文件导入,我应该只在其中使用文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM