我试图导入使用Webpack编译的TypeScript模块,但是出现以下错误,
TypeError: messages.sayHello is not a function
at Object.<anonymous> (/Users/gaurangdave/workspace/experiments/tmp/ts_modules/main.js:3:22)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
下面是TypeScript,
const sayHello = name => {
console.log(`Hello ${name}`);
};
export default { sayHello };
下面是Webpack配置
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'none',
entry: {
greetings: './greetings.ts',
},
target: 'node',
resolve: {
extensions: ['.ts', '.js']
},
optimization: {
minimize: false
},
output: {
// Puts the output at the root of the dist folder
path: path.join(__dirname, 'dist'),
filename: '[name].js'
},
module: {
rules: [{
test: /\.ts$/,
loader: 'ts-loader'
}]
},
plugins: []
}
有点受阻,有人可以丢点光吗?
谢谢 高朗
编辑:
我正在按如下方式调用sayHello函数
const greetings = require('./dist/greetings');
greetings.sayHello();