【问题标题】:Angular js install external library failed to instantiate module npmAngular js安装外部库无法实例化模块npm
【发布时间】:2021-10-06 03:50:01
【问题描述】:

在我的 AngularJS 项目中,我尝试添加外部库,例如 chart.js。 所以我开始使用命令在 node_modules 中添加新包:

npm install chart.js --save

在下载过程中我看到这个警告:

`--chart.js@3.5.0

npm WARN 可选跳过可选依赖:fsevents@^1.2.7 (node_modules\chokidar\node_modules\fsevents):npm WARN notsup 跳过可选依赖:不支持的平台 fsevents@1.2.13: 想要 {"os":"darwin","arch":"any"}(当前: {"os":"w"win32","arch":"x64"}) npm WARN 可选 SKIPPING OPTIONAL 依赖:fsevents@^1.0.0 (node_modules\karma\node_modules\chokidar\node_modules\fsevents):npm 警告 notsup 跳过可选依赖:不支持的平台 fsevents@1.2.13: 想要 {"os":"darwin","arch":"any"}(当前: {"os":"w"win32","arch":"x64"})

尽管有警告,我还是在我的 node_modules 文件夹中找到了 chart.js。

之后,如指南中所述,我在控制器中添加对新模块的引用:

angular
  .module('myportal',['chart.js'])
  .controller(
    'PortalController',
      [
      '$scope',function($scope){
      // other functions
    

但是当我尝试启动网络应用程序时,我看到了停止加载应用程序的错误:

Error: [$injector:modulerr] Failed to instantiate module chart.js due to: Error: [$injector:nomod] Module 'chart.js' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.4.7/$injector/nomod?p0=chart.js
    at http://localhost:3001/lib/angular/angular.js:68:12
    at http://localhost:3001/lib/angular/angular.js:1986:17
    at ensure (http://localhost:3001/lib/angular/angular.js:1910:38)
    at module (http://localhost:3001/lib/angular/angular.js:1984:14)
    at http://localhost:3001/lib/angular/angular.js:4390:22
    at forEach (http://localhost:3001/lib/angular/angular.js:336:20)
    at loadModules (http://localhost:3001/lib/angular/angular.js:4374:5)
    at http://localhost:3001/lib/angular/angular.js:4391:40
    at forEach (http://localhost:3001/lib/angular/angular.js:336:20)
    at loadModules (http://localhost:3001/lib/angular/angular.js:4374:5)

我是否在控制器或其他配置文件中遗漏了某些内容?或者它是否取决于我忘记的其他东西?(我正在使用 grunt 并且我没有在 index.html 中添加带有路径的脚本标记)谢谢

【问题讨论】:

    标签: javascript node.js angularjs npm chart.js


    【解决方案1】:

    您可能想尝试将 chart.js 的位置添加到您的 index.html 中。

    <script src='node_modules/chartjs/chartjs.js'></script>
    

    以上是对文件路径的猜测。 npm 会将其安装到您的位置目录,但 AngularJS 不知道该文件在哪里。

    【讨论】:

    • 感谢您的建议,我会避免在 index.html 中添加
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多