简体   繁体   English

Babel +打字稿+ NPM模块+ Webpack

[英]Babel + Typescript + NPM Modules + Webpack

Hello I try to require angular from my typescript script, and I get this error: 您好,我尝试从我的打字稿脚本中要求角度,并且出现此错误:

error TS2307: Cannot find module 'angular'.

Here is my webpack config file: 这是我的webpack配置文件:

    context: __dirname + '/client/js',
    entry: './script.ts',
    output: {
        filename: 'build.js',
        path: options.BUILD ? 'dist' : 'dev'
    },
    module: {
        loaders: [
            {test: /\.ts(x?)$/, loader: 'babel-loader!ts-loader'}
        ]
    }

EDIT: 编辑:

Then, in my script.ts file, I do: 然后,在我的script.ts文件中,执行以下操作:

import angular from 'angular'; 

or even var angular = require('angular'); 甚至var angular = require('angular');

gives me the same result 给我相同的结果

I don't have any tsconfig.json file 我没有任何tsconfig.json文件

Thank you ! 谢谢 !

I don't have any tsconfig.json file 我没有任何tsconfig.json文件

You need a tsconfig.json file. 您需要一个tsconfig.json文件。 See ts-loader readme : https://github.com/TypeStrong/ts-loader#configuration 参见ts-loader自述文件: https : //github.com/TypeStrong/ts-loader#configuration

Example with babel 巴别塔的例子

Checkout : https://github.com/TypeStrong/ts-loader/issues/93 结帐: https : //github.com/TypeStrong/ts-loader/issues/93

first in your terminal type tsc --init then add to types array in tsconfig.json angular declaration the last thing you have to do is import all from angular import * as angular from "angular" . 先在你的终端类型tsc --init ,然后添加types数组中tsconfig.json角声明中,您需要做的最后一件事是进口全部来自角import * as angular from "angular" You can use angular declaration eg $location: angular.ILocationService 您可以使用角度声明,例如$location: angular.ILocationService

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM