当前位置: 首页 > 面试题库 >

Angular2-Angular-CLI安装lodash-找不到模块

蒋寒
2023-03-14
问题内容

Mac OSX El capitan | angular-cli:0.1.0 | 节点:5.4.0 | 操作系统:darwin x64

我尝试根据angular-cli Wiki安装第三方npm模块:https : //github.com/angular/angular-cli/wiki/3rd-party-
libs
但失败。我已经为此苦苦挣扎了好几天了,非常感谢您的帮助。

得到错误的步骤:

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 (将破折号正确添加到dist /
vendor中)

system-config.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)

编辑:根据下面的评论更多信息

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>

问题答案:

有了稳定的版本和当前的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"到全局脚本的to列表(添加到list
apps[0].scripts)。

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



 类似资料:
  • 问题内容: 今天,我尝试了解有关Google Web Starter Kit的更多信息,因此我遵循了这些说明,经过一番斗争和问题,我只是尝试启动本地服务器(我们将要研究的第一个任务是:。)并收到以下错误消息: C:\ gwsk> gulp服务 老实说,我在这里完全迷路了,因此非常感谢您的帮助。我是node.js的新手,我只是想让它尝试GWSK,但令人头疼:(…我是一名Web设计人员,而不是开发人员

  • 今天我试图了解更多关于Google Web Starter Kit的信息,所以我按照这些说明进行了操作,在经历了大量的斗争和问题之后,我尝试启动一个本地服务器(我们将看到的第一个任务是:)并收到此错误: C:\gwsk 老实说,我完全迷失在这里,所以欢迎任何帮助。我是新来的node.js,吞咽,我只是想尝试GWSK,但变成头痛:(...我是一个网页设计师不是开发者......

  • 问题内容: 我正在使用angular2和gulp开发一个节点应用程序。我编写了一个组件文件login.ts,如下所示: 我的bootstrap.ts文件是: 但是当我编译这些文件时,出现以下错误: 这是我的tsconfig.json: 我已经使用以下命令安装了angular2的类型: 请帮助纠正错误。 问题答案: @simon错误说的是输入错误。但是对于其他进口商品,我已经发布了此答案,可能对其他

  • 我对Angular2是新手。我正在学习。这是第一个简单的教程,叫做猜数字 我使用了github提供的示例https://github.com/chandermani/angular2byexample/tree/master/guessthenumber中描述的代码。 它在浏览器中加载得很好。我有问题的是IntelliJ设置。 IntelliJ返回以下错误。如截图所示。找不到模块“@Angular

  • 我正在尝试从github安装Exscript。 当我试图加载它时,python找不到它: 但是,当我尝试用setup在同一个虚拟环境中安装它时。py它成功安装并加载。 我做错了什么?

  • 我不能使用NodeJS中的命令提示符来使用。运行时出现以下错误: