簡體   English   中英

打字稿:意外的令牌導入

[英]Typescript: Unexpected token import

剛開始使用打字稿。 不幸的是,當我嘗試為生產而構建時,它失敗了。

首先我跑

tsc

這通過沒有任何錯誤,但是當我嘗試運行構建文件時,我收到導入錯誤

node build/index.js

我得到的錯誤如下:

[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)

下面是我的 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
   }
}

我正在使用節點v8.9.3

如果您使用 TypeORM,那么您的 ormconfig 可能會出現問題。 您的配置文件可能在entity部分包含類似src/entities/*.ts路徑。 所以它導致需要來自src文件夾的*.ts文件,而不是來自dist文件夾。

使用 NodeJs 時,您的tsconfig.json應如下所示:

{
    "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
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM