【问题标题】:Angular2 - Angular-CLI installing lodash - Cannot find moduleAngular2 - Angular-CLI 安装 lodash - 找不到模块
【发布时间】:2016-10-09 07:44:21
【问题描述】:

Mac OSX El 头|角-cli:0.1.0 |节点:5.4.0 |操作系统:达尔文 x64

我尝试根据 angular-cli wiki:https://github.com/angular/angular-cli/wiki/3rd-party-libs 安装第 3 方 npm 模块,但失败了。我已经为此苦苦挣扎了好几天,如果有任何帮助,我将不胜感激。

获取错误的步骤:

ng new lodashtest3 cd lodashtest3 npm install lodash --save typings install lodash --ambient --save

angular-cli-build.json:

module.exports = function(defaults) { return new Angular2App(defaults, { vendorNpmFiles: [ ... 'lodash/**/*.js' ] }); }; ng build (lodash 被正确添加到 dist/vendor 中)

系统配置.ts:

/** Map relative paths to URLs. */
 const map: any = {
   'lodash': 'vendor/lodash/lodash.js'
 };

 /** User packages configuration. */
 const packages: any = {
   'lodash': {
     format: 'cjs'
   }
 };

(全部按照规范中的 https://github.com/angular/angular-cli/wiki/3rd-party-libs) 注意 - 我在这里尝试了所有我能想到的配置设置,都给出了相同的结果。

lodashtest3.component.ts: import * as _ from 'lodash';

ng build

Could not start watchman; falling back to NodeWatcher for file system events.
Visit http://ember-cli.com/user-guide/#watchman for more info.
Build failed.
The Broccoli Plugin: [BroccoliTypeScriptCompiler] failed with:
Error: Typescript found the following errors:
  /Users/danielmattsson/git/lodashtest3/tmp/broccoli_type_script_compiler-input_base_path-g2lDIaq6.tmp/0/src/app/lodashtest3.component.ts (2, 20): Cannot find module 'lodash'.
    at BroccoliTypeScriptCompiler._doIncrementalBuild (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/lib/broccoli/broccoli-typescript.js:115:19)
    at BroccoliTypeScriptCompiler.build (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/lib/broccoli/broccoli-typescript.js:43:10)
    at /Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/broccoli-caching-writer/index.js:152:21
    at lib$rsvp$$internal$$tryCatch (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:1036:16)
    at lib$rsvp$$internal$$invokeCallback (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:1048:17)
    at lib$rsvp$$internal$$publish (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:1019:11)
    at lib$rsvp$asap$$flush (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/broccoli-caching-writer/node_modules/rsvp/dist/rsvp.js:1198:9)
    at nextTickCallbackWith0Args (node.js:456:9)
    at process._tickCallback (node.js:385:13)

The broccoli plugin was instantiated at:
    at BroccoliTypeScriptCompiler.Plugin (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/broccoli-caching-writer/node_modules/broccoli-plugin/index.js:10:31)
    at BroccoliTypeScriptCompiler.CachingWriter [as constructor] (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/broccoli-caching-writer/index.js:21:10)
    at BroccoliTypeScriptCompiler (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/lib/broccoli/broccoli-typescript.js:26:49)
    at Angular2App._getTsTree (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/lib/broccoli/angular2-app.js:280:18)
    at Angular2App._buildTree (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/lib/broccoli/angular2-app.js:101:23)
    at new Angular2App (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/lib/broccoli/angular2-app.js:42:23)
    at module.exports (/Users/danielmattsson/git/lodashtest3/angular-cli-build.js:6:10)
    at Class.module.exports.Task.extend.setupBroccoliBuilder (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/angular-cli/lib/models/builder.js:55:19)
    at Class.module.exports.Task.extend.init (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/angular-cli/lib/models/builder.js:89:10)
    at new Class (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/angular-cli/node_modules/core-object/core-object.js:18:12)
    at Class.module.exports.Task.extend.run (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/angular-cli/lib/tasks/build.js:15:19)
    at /Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/angular-cli/lib/commands/build.js:32:24
    at lib$rsvp$$internal$$tryCatch (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/angular-cli/node_modules/rsvp/dist/rsvp.js:1036:16)
    at lib$rsvp$$internal$$invokeCallback (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/angular-cli/node_modules/rsvp/dist/rsvp.js:1048:17)
    at /Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/angular-cli/node_modules/rsvp/dist/rsvp.js:331:11
    at lib$rsvp$asap$$flush (/Users/danielmattsson/git/lodashtest3/node_modules/angular-cli/node_modules/angular-cli/node_modules/rsvp/dist/rsvp.js:1198:9)

编辑:根据下面的 cmets 获得更多信息

dist/index.html:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Lodashtest3</title>
  <base href="/">

  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Service worker support is disabled by default.
       Install the worker script and uncomment to enable.
       Only enable service workers in production.
  <script type="text/javascript">
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.register('/worker.js').catch(function(err) {
        console.log('Error installing service worker: ', err);
      });
    }
  </script>
  -->
</head>
<body>
  <lodashtest3-app>Loading...</lodashtest3-app>

  <script src="vendor/es6-shim/es6-shim.js"></script>
  <script src="vendor/reflect-metadata/Reflect.js"></script>
  <script src="vendor/systemjs/dist/system.src.js"></script>
  <script src="vendor/zone.js/dist/zone.js"></script>

  <script>
    System.import('system-config.js').then(function () {
      System.import('main');
    }).catch(console.error.bind(console));
  </script>
</body>
</html>

【问题讨论】:

  • 您能否检查您的 dist/index.html ,其中显示 lodash.js 路径,或者您能否发布您的 dist/index.html 代码。我对 jquery 有类似的问题。所以我可以帮忙:)
  • 请检查您提供的'lodash': 'vendor/lodash/lodash.js'的路径是否正确?因为npm install会在node_modules目录下安装lodash
  • @pdfarhad 我已将 dist/index.html 添加到原始帖子中。
  • @HardipsinhJadeja 是的 lodash 安装在 node_modules 中,但是 angular-cli/webpack 然后将它放在 dist/vendor/lodash/lodash.js 中。规范 (github.com/angular/angular-cli/wiki/3rd-party-libs) 也说 'vendor/xx',所以我认为这是正确的。

标签: angularjs lodash systemjs npm-install angular-cli


【解决方案1】:

[更新答案] 新版之后angular-cli (1.0.0-beta.15):

添加

npm install lodash --save
npm install @types/lodash --save-dev

然后将库添加到 angular-cli.json 到全局脚本列表中(添加 "../node_modules/lodash/lodash.js" 到列表 apps[0].scripts)。

在你想要使用的组件中,试试这种方式

declare var _:any;

@Component({
})
export class YourComponent {
  ngOnInit() {
     console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
  }
}

之前:angular-cli (1.0.0-beta.15):

在 src/index.html 中添加这一行

  <script src="/vendor/lodash/lodash.js" type="text/javascript"></script>

在你想要使用的组件中,试试这种方式

declare var _:any;

@Component({
})
export class YourComponent {
  ngOnInit() {
     console.log(_.chunk(['a', 'b', 'c', 'd'], 2));
  }
}

我立即尝试,它对我有用

【讨论】:

  • 非常感谢,是的,它有效,我将使用它作为解决方法,直到他们获得对 3rd 方库的更好支持。 webpack/systemjs 的目的是编译库并加载它们(因此您不必向 index.html 添加任何内容)。但是,它似乎非常不稳定,所以我想像这样的解决方案必须这样做,直到它变得更好。再次感谢!
  • @DanielMattsson:我更新了我的答案。我之前的回答是针对您的具体问题。它被你接受了。随着新版本的到来,我更新了答案
【解决方案2】:

为了在 Angular CLI 和 System.js 中支持第 3 方库,您必须在 system.config.tsangular-cli-build.js 上手动指定它们。 我已经回答了here,并以lodash 和其他依赖项为例。

【讨论】:

    【解决方案3】:

    使用稳定版本和当前的 angular-cli (1.0.0-beta.15),只需添加 npm 包和类型定义

    npm install lodash --save
    npm install @types/lodash --save-dev
    

    对于 1.0.0-beta.15 等早期版本,需要下一个。当前版本不需要它:

    将库添加到 angular-cli.json 到全局脚本列表(添加 "../node_modules/lodash/lodash.js" 到列表 apps[0].scripts)。

    https://github.com/angular/angular-cli#global-library-installation

    【讨论】:

    • 我将您的答案更改为正确的答案,因为从最新版本开始,这已更改。感谢上帝,他们让这件事变得更容易:)
    • 从 1.0.0-beta.26 开始,我仍然需要使用 declare const _:any;这也会破坏自动完成功能。
    • 当前版本的 cli (@angular/cli@1.4.4) @Eric Liprandi 的回答中指定的过程应该更新为正确的,因为不再需要在全局 (angular-cli.json) 上添加整个库.使用import 语法将其添加到范围内是一种更好的做法。
    【解决方案4】:

    FWIW,从今天开始 (1.0.0-beta.26),不再需要添加 scripts 条目。

    只需使用以下命令将正确的条目添加到package.json

    npm i --save lodash
    npm i --save-dev @types/lodash
    

    然后,在您的 TypeScript 代码中,使用:

    import * as _ from 'lodash';
    

    代码运行良好。

    就我而言,实际上添加 scripts 条目会导致问题。

    【讨论】:

      【解决方案5】:

      截至 2017 年 4 月 15 日,@angular/cli 1.0.0:

      您需要非常明确您安装的版本,否则 TypeScript 绑定会导致各种故障。我所做的是在package.json 中更新我的devDependencies 如下:

      "@types/lodash": "ts2.0"
      

      【讨论】:

      • 这是对我有用的 1.0.0-beta.28.3、lodash 4.15.0、@types/lodash ts2.0。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 2016-09-25
      • 1970-01-01
      • 2018-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多