当我尝试编译.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)
编译我使用以下配置文件。
的WebPack:
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编译。所以我仍然想知道如何解决上面提到的错误。
答案 0 :(得分:3)
请在您的tsconfig.json中将noEmit设置为false。 默认情况下,它设置为true,一旦将其更改为false,我们可能不会收到此错误。
“ noEmit”:false;
答案 1 :(得分:0)
在 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,
},
},
},
],
},
]