TypeError:不是构造函数[Webpack Typescript]导入到nodeJS

时间:2017-11-06 11:49:27

标签: javascript node.js typescript webpack webstorm

从WebStorm 2017.2.5创建主题Node.js Express App

Javascript语言版本ECMAScript 5.1

测试webpack @ 2和webpack @ 3时出现

错误

我不是来自导入类的新obj

我尝试默认导出和任何

抱歉,我的小技巧

---------- bin
---------- ctrl/
               main.ts
               control.ts
               interface.ts
---------- dist / main.js
---------- node_modules /
---------- public /
---------- routes / index.js
---------- views  /
---------- app.js
---------- package.json
---------- package-lock.json
---------- webpack.config.js
---------- tsconfig

main.ts

import {controlA} from from './control';
export default class mainA{
   protected token:string;
   protected controlOBJ:any;
   constructor(_token:string){
     this.token = _token;
     console.log('token'+this.token);
     this.controlOBJ = new controlA(this.token);
   }
}

control.ts

  export class controlA{
  protected verifyToken:string;
    constructor(_verifyToken:string){
      this.verifyToken = _verifyToken;
      console.log('verifyToken'+this.verifyToken);
    }
  }

index.js

 var express = require('express');
 var router = express.Router();
 //////////////
 var mainA = require('../dist/main).default; 
 new mainA('token');
 // try not work TypeError: mainA is not a constructor

 var mainA = require('../dist/main).mainA;   // try not work 
 new mainA('token');
 // try not work TypeError: mainA is not a constructor

 var mainA = require('../dist/main);
 new mainA.mainA('token');
 // try not work TypeError: mainA is not a constructor

 router.get('/', function(req, res, next) {
    res.render('index', { title: 'Express' });
 });

 module.exports = router;

webpack.config.js

var path = require('path');
var webpack = require('webpack');
module.exports = {
    entry: './ctrl/main.ts',
    resolve: {
      extensions: [".webpack",".tsx", ".ts", ".js"]
    },
    module: {
      loaders: [
        { test: /.ts$/, loader: 'ts-loader' , exclude: /node_modules/} //awesome-typescript-loader
      ]
    },
    output: {
      filename: 'main.js',
      path: path.resolve(__dirname, 'dist')
    }
} 

进行****测试
   module: {
      loaders: [
         { test: /\.ts$/, loader: 'ts-loader' }
      ]
   }

无法正常工作

    module: {
    rules: [
        {
            test: /\.ts$/,
             exclude: path.resolve(__dirname, 'node_modules'),
            use:  {
                loader: 'babel-loader',
                options: {
                    presets: [ 'env' ],
                    plugins: [ 'transform-runtime' ]
                }
            }
        }
    ]
},

无法正常工作

 var babelOptions = {
     "presets": "es5"
 };
  module: {
    rules: [{
        test: /\.ts(x?)$/,
        exclude: /node_modules/,
        use: [
            {
                loader: 'babel-loader',
                options: babelOptions
            },
            {
                loader: 'ts-loader'
            }
        ]
    }, {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
            {
                loader: 'babel-loader',
                options: babelOptions
            }
        ]
    }]
},

无法正常工作

tsconfig

{
 "compileOnSave": true,
 "compilerOptions": {
 "target": "es5",
 "module": "commonjs",
 "noImplicitAny": false,
 "noEmitOnError": false,
 "removeComments": true,
 "sourceMap": false,
 "inlineSources": false,
 "preserveConstEnums": true,
 "declaration": false,
 "noEmit": false,
 "noLib": false,
 "allowJs": false
},
 "exclude": [
 "*"
 ],
 "files": [
 "ctrl/interface.ts",
 "ctrl/control.ts",
 "ctrl/main.ts"
 ]
}

请帮助我

尝试从webpack(dist / main.js)中删除行标题生成器 为什么? T T

0 个答案:

没有答案