打字稿构建从 node_modules 文件夹中获取错误

Posted

技术标签:

【中文标题】打字稿构建从 node_modules 文件夹中获取错误【英文标题】:Typescript build getting errors from node_modules folder 【发布时间】:2016-05-24 18:10:09 【问题描述】:

我正在运行打字稿构建并在 node_modules 中出现错误。为什么不忽略此文件夹?我在 tsconfig.json 的排除部分中有它。真正奇怪的是,我有另一个项目进行了文件比较,即使 gulpfile.js、tsconfig.json 和 node_modules 文件夹相同,它也不会抛出这些错误。我还能检查什么?

错误:

c:/Dev/streak-maker/node_modules/angular2/src/core/change_detection/parser/locals.d.ts(3,14): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/change_detection/parser/locals.d.ts(4,42): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/debug/debug_node.d.ts(14,13): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/debug/debug_node.d.ts(24,17): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/debug/debug_node.d.ts(25,17): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/di/provider.d.ts(436,103): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/di/provider.d.ts(436,135): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/render/api.d.ts(13,13): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/render/api.d.ts(14,84): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(1,25): error TS2304: Cannot find name 'MapConstructor'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(2,25): error TS2304: Cannot find name 'SetConstructor'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(4,27): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(4,39): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(7,9): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(8,30): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(11,43): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(12,27): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(14,23): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(15,25): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(95,41): error TS2304: Cannot find name 'Set'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(96,22): error TS2304: Cannot find name 'Set'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(97,25): error TS2304: Cannot find name 'Set'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/lang.d.ts(13,17): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/lang.d.ts(14,17): error TS2304: Cannot find name 'Set'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/lang.d.ts(78,59): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(1,10): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(3,14): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(8,32): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(9,38): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(10,35): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(10,93): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(11,34): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(12,32): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(12,149): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/promise.d.ts(13,43): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/angular2/src/http/headers.d.ts(43,59): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/http/url_search_params.d.ts(11,16): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/platform/browser/browser_adapter.d.ts(75,33): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/platform/dom/dom_adapter.d.ts(85,42): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/rxjs/CoreOperators.d.ts(22,67): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/CoreOperators.d.ts(72,67): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/CoreOperators.d.ts(77,31): error TS2304: Cannot find name 'PromiseConstructor'.
c:/Dev/streak-maker/node_modules/rxjs/CoreOperators.d.ts(77,54): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(65,67): error TS2304: Cannot find name 'PromiseConstructor'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(65,88): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(72,84): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(77,38): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(100,66): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(154,66): error TS2304: Cannot find name 'Promise'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(159,31): error TS2304: Cannot find name 'PromiseConstructor'.
c:/Dev/streak-maker/node_modules/rxjs/Observable.d.ts(159,54): error TS2304: Cannot find name 'Promise'.

tsconfig.js


  "version": 3,
  "compilerOptions": 
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  ,
  "exclude": [
    "node_modules",
    "jspm_packages"
  ]

gulpfile.js(我正在运行 build-typescript 任务 - 我在命令行输入 tsc 时遇到同样的错误)

/// <binding Build='default' />

var del = require('del'),
    gulp = require("gulp"),
    ts = require('gulp-typescript'),
    watch = require('gulp-watch');

var webproj = "./src/StreakMaker.Web/";
var webroot = webproj + "wwwroot/";
var appsource = webproj + "App/";
var appout = webroot + "app/";
var jspmsource = "./jspm_packages/";
var jspmout = webroot + "jspm_packages/";

var paths = 
    webroot: webroot,
    src: appsource,
    app: appout,
    jspm: jspmsource,
    jspm_out: jspmout
;

gulp.task('watch', ['watch-typescript', 'watch-html']);

gulp.task('watch-typescript', function()
    gulp.watch(paths.src + '/**/*.ts', ['build-typescript']);
);

gulp.task('clean-typescript', function () 
    del([paths.app + '/**/*.ts']);
);

gulp.task('build-typescript', ['clean-typescript'], function () 
    var tsProject = ts.createProject('./tsconfig.json');

    gulp.src(paths.src + '/**/*.ts')
        .pipe(ts(tsProject))
        .pipe(gulp.dest(paths.app));
);

gulp.task('watch-html', function () 
    gulp.watch(paths.src + '/**/*.html', ['copy-html']);
);

gulp.task('clean-html', function () 
    del([paths.app + '/**/*.html']);
);

gulp.task('copy-html', ['clean-html'], function () 
    gulp.src(paths.src + '/**/*.html')
        .pipe(gulp.dest(paths.app));
);

gulp.task('copy-jspm', ['clean-jspm', 'copy-config'], function() 
    gulp.src(paths.jspm + "**/*.js,css,map")
        .pipe(gulp.dest(paths.jspm_out));
);

gulp.task('clean-jspm', function()
    del([paths.jspm_out + "**/*.*"]); 
);

gulp.task('copy-config', ['clean-config'], function()
    gulp.src("./config.js")
        .pipe(gulp.dest(paths.webroot));
);

gulp.task('clean-config', function()
    del(paths.webroot + 'config.js'); 
);

gulp.task('default', ['build-typescript', 'copy-html', 'copy-jspm']);

【问题讨论】:

【参考方案1】:

我也在为此苦苦挣扎。

我在包含导入 bootstrap 行的 TS 文件的顶部添加了以下内容:

///<reference path="../node_modules/angular2/typings/browser.d.ts"/> 

该文件的路径当然可能不同。

【讨论】:

这非常有用 (+1),如果遵循 quickstart on angular.io,直接引用***类型也可以:/// &lt;reference path="../typings/browser.d.ts" /&gt; 有没有人找到解决方案...我尝试了所有方法。我得到的只是找不到地图、设置、期望 .... 好吧,认为 标签是一种不好的做法。这是 Minko Gechev 写的关于这个确切错误的article。 Here 是另一篇解释整个打包过程的文章。【参考方案2】:

虽然这个解决方案

/// <reference path="../typings/browser.d.ts" />
import bootstrap from 'angular2/platform/browser';
import AppComponent from './app/main';

bootstrap(AppComponent);

效果很好,我更喜欢使用 gulpjs,但不是用 cmets 填充源文件,而是直接将类型文件添加到相应 gulpjs 任务的源中,例如:

gulp.task('typescript', function () 
  return gulp
    .src([
      'typings/browser.d.ts',
      tsSrc + '**/*.ts'
    ])
    .pipe(sourcemaps.init())
    .pipe(typescript(tscConfig.compilerOptions))
    .pipe(sourcemaps.write('.'))
    .pipe(gulp.dest(appSrc + 'js/'));
);

【讨论】:

【参考方案3】:

在 RC1 演示中,垫片由 core-js 模块提供,类型定义由 typings 工具管理。将typings/index.d.ts 添加到gulp.src 应该可以解决问题:

function buildTypeScriptFn(files) 
  files = files || ['app/**/*.ts','typings/index.d.ts'];

  return function () 
    var tsResult = gulp.src(files)
      .pipe(changed(paths.dirs.build,  extension: '.js' ))
      .pipe(ts(tscConfig.compilerOptions));

    return merge(tsResult.dts, tsResult.js)
      .pipe(gulp.dest(paths.dirs.build));
  


gulp.task('ts', buildTypeScriptFn());

【讨论】:

【参考方案4】:

我遇到了同样的问题,只是尝试不使用“npm start”运行, 但使用“npm run lite”。使用起来更方便。

【讨论】:

这对我有用。以后会不会有什么问题?【参考方案5】:

如果您的目标是 ES5,请将“node_modules/typescript/lib/lib.es6.d.ts”添加到 tsconfig.json 文件中:


    "compilerOptions": 
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "outDir": "built",
        "rootDir": ".",
        "sourceMap": false
    ,
    "files": [
      "helloworld.ts",
      "node_modules/typescript/lib/lib.es6.d.ts"
    ],
    "exclude": [
        "node_modules"
    ]

【讨论】:

新手评论。在 Visual Studio 2015 中执行此操作让我出现了 43k 新的打字稿构建错误。 当一些模块说他们找不到它们的依赖项时,这对我有用。

以上是关于打字稿构建从 node_modules 文件夹中获取错误的主要内容,如果未能解决你的问题,请参考以下文章

如何在 ./node_modules/@types 之外使用打字稿定义文件

如何让 VS Code 智能感知在多个 node_modules 文件夹中搜索打字稿

打字稿打字,打字文件夹是啥

如何设置我的VS 2013 TypeScript项目来编译我的打字稿文件但排除我的node_modules?

打字稿:设置“node_modules”目录路径

打字稿找不到本地链接的模块?