webpack 模块构建失败:错误:Typescript未发出的输出

wgx48brx  于 2023-02-04  发布在  Webpack
关注(0)|答案(4)|浏览(429)

当我尝试编译一个.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']),
        ]
    }
};

typescript :

{
    "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编译。所以我仍然想知道如何解决上面提到的错误。

hiz5n14c

hiz5n14c1#

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

8tntrjer

8tntrjer2#

在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,
              },
            },
          },
        ],
      },
    ]
6qqygrtg

6qqygrtg3#

使用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/

xpcnnkqh

xpcnnkqh4#

在我的例子中,我必须从tsconfig.json中删除"emitDeclarationOnly": true
(or只需将其设置为false
启用此选项后,它将只输出d.ts文件,而不输出JavaScript文件。
阅读更多信息:https://www.typescriptlang.org/tsconfig#emitDeclarationOnly

相关问题