【问题标题】:Unable to find node_modules on client side in angular 1在角度 1 中无法在客户端找到 node_modules
【发布时间】:2017-06-15 20:49:24
【问题描述】:

我正在尝试在我的 Angular 1 应用程序中加载一个非 Angular 库。

index.html 有以下脚本:

<head>
<script type="text/javascript" src="../node_modules/@mylib/index.js" charset="utf-8"></script>
<script type="text/javascript"src="../bower_components/myOtherlib/index.js"charset="utf-8"></script>
</head>

我的目录结构是:

MyApp
- src
  - app
    - index.js
  - index.html
- node_modules
- bower_components
- package.json
- bower.json
- test
- gulpfile.js

我可以从 index.html 访问 bower_components,但不能访问 node_modules。 所以我可以加载 myOtherlib/index.js 但不能加载 @mylib/index.js

当我加载 index.html 时出现错误:

GET http://localhost:3000/node_modules/@mylib/index.js 404(未找到)

我正在使用 IntelliJ。

如果有人能帮我解决这个问题,那将会很有帮助。感谢您的时间和帮助。

【问题讨论】:

  • 节点模块通常是服务器端...究竟你想从浏览器中访问什么?
  • 你试过运行npm install吗?
  • 我有一个库,我想在 index.html 中访问它并在前端使用库中的类。
  • 您只需将所述文件移动到浏览器可用的位置。您不需要捆绑器,gulp 可用于将此文件移动到您的构建目录中。
  • 我的观点仍然成立;如果这是一个打算在浏览器中使用的库,它应该已经将自身复制到浏览器可访问的目录中。在不知道库是什么的情况下,不清楚为什么不是这种情况,甚至还不清楚哪些文件需要移动到哪里。

标签: javascript angularjs node-modules


【解决方案1】:

这是因为在前端(客户端)中,您使用 bower 来管理依赖项,以便在成功运行 gulp 任务后不会包含节点模块。您需要修改 gulp 任务以包含节点模块。 这是因为从您的示例中我了解到您正在使用 gulp 作为任务运行程序,当您运行 gulp serve(或运行应用程序的任务)时,将在该凉亭中创建 dist 文件夹,但不包含 node_modules。您的所有应用程序正在通过 dist 文件夹运行。

最后你需要修改 gulpfile.js 以包含 node_modules。

这将解决您的问题。

然后像这样包含 node_modules/dist/lodash.js

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 2023-04-07
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    相关资源
    最近更新 更多