【问题标题】:Module build failed: Error: Typescript emitted no output for模块构建失败:错误:Typescript 没有输出
【发布时间】:2018-04-22 18:02:36
【问题描述】:

当我尝试编译 .ts 文件时,出现以下错误:

Module build failed: Error: Typescript emitted no output for C:\xampp\htdocs\node-api\src\js\server.ts. 
at successLoader (C:\xampp\htdocs\node-api\node_modules\ts-loader\dist\index.js:39:15)
at Object.loader (C:\xampp\htdocs\node-api\node_modules\ts-loader\dist\index.js:21:12)

为了编译,我使用以下配置文件。

网页包:

const path = require( 'path' ),
    CleanWebpackPlugin = require( 'clean-webpack-plugin' );

module.exports = env => {
    return {
        mode: env.dev ? 'development' : 'production',
        entry: {
            'server': './src/js/server.ts'
        },
        output: {
            path: __dirname,
            filename: './dist/js/[name].js',
        },
        externals: '/node_modules',
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: ['/node_modules/', '/src/scss/'],
                    use: [
                        'babel-loader'
                    ]
                },
                {
                    test: /\.ts(x?)$/,
                    exclude: ['/node_modules/', '/src/scss/'],
                    use: [
                        'babel-loader',
                        'ts-loader',
                    ]
                },
                {
                    test:  /\.json$/,
                    loader: 'json-loader'
                },
            ]
        },
        resolve: {
            extensions: ['.ts', '.tsx', '.js' ],
            alias: {
                '@': path.resolve(__dirname, './src/js')
            }
        },
        plugins: [
            new CleanWebpackPlugin(['./dist/js', './dist/css']),
        ]
    }
};

打字稿:

{
    "compilerOptions": {
        "removeComments": true,
        "preserveConstEnums": true,
        "allowJs": true,
        "outDir": "./dist/js",
        "target": "es5",
        "moduleResolution": "node",
        "module": "es2015",
        "lib": [
            "es2015",
            "es2016"
        ]
    },
    "exclude": [
        "node_modules"
    ]
}

通天塔:

{
    "presets": [
        [
            "env", {
                "targets": {
                    "node": "current"
                }
            }
        ],
        "stage-2", "es2015"
    ],
    "plugins": ["dynamic-import-node"]
}

正如在其他一些问题中所建议的那样,我已经更改了解析扩展的顺序,但这并没有解决它(.js 之前的.ts)。 Typescript 2.8.3 与 Node 8.11.1 和 Mongoose 5.0.15 结合使用,由 Webpack 4.6 编译。所以我仍然想知道如何解决上面提到的错误。

【问题讨论】:

  • 发布你的 tsconfig.json
  • @Sajeetharan 它在Typescript:
  • 你可以尝试在你的 tsconfig 中添加 "files": [ "./src/components/index.d.ts" ]
  • @Sajeetharan 试过了,但也没用
  • 好吧 index.ts 而不是 index.d.ts

标签: typescript webpack ts-loader


【解决方案1】:

请在您的tsconfig.json 中将noEmit 设置为false。 默认情况下它设置为true,一旦我们将其更改为false,我们可能不会收到此错误。

"noEmit": false

【讨论】:

  • 尝试将此添加为评论或详细说明您的答案。
  • 但是……但是……为什么?
【解决方案2】:

在 webpack 配置中像这样覆盖 compilerOptions(使用 ts-loader 时):

    rules: [
      {
        test: /\.[jt]s$/,
        exclude: /(node_modules)/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: ['@babel/preset-env'],
            },
          },
          {
            loader: 'ts-loader',
            options: {
              compilerOptions: {
                noEmit: false,
              },
            },
          },
        ],
      },
    ]

【讨论】:

  • webpack.config.json, Webpack 5.47版本中无法设置此选项
【解决方案3】:

使用 webpack 5 和 Typescript 4.5,我可以使用以下内容运行 expressjs + prisma 服务器:

webpack.config.js:

const path = require('path');

module.exports = {
  entry: './src/index.ts',
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: [
          {
            loader: 'ts-loader',
            options: {
              compilerOptions: {noEmit: false},
            }
          }
        ],
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

tsconfig.json:

{
  "compilerOptions": {
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "CommonJS",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "jsx": "preserve",
    "incremental": true
  },
  "include": [
    "**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

来源:https://webpack.js.org/guides/typescript/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-11
    • 2017-07-19
    • 2021-05-07
    • 2017-10-07
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多