javascript - 无法在 Angular 1 的客户端找到 node_modules

标签 javascript angularjs node-modules

我正在尝试在我的 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 时,出现错误:

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

我正在使用 IntelliJ。

如果有人能帮我解决这个问题,那就太好了。感谢您的时间和帮助。

最佳答案

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

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

这将解决您的问题。

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

关于javascript - 无法在 Angular 1 的客户端找到 node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41940266/

相关文章:

javascript - 扩展 react 组件的问题/问题

javascript - 模态 Controller 未调用

angularjs - 如何模拟提供商

javascript - 如何使用粗箭头表示法编写异步函数模块函数

javascript - 为什么我的 cmd 给出找不到 Node 模块的错误?

javascript - Jquery:无法让动画工作

javascript - 为什么数组元素没有被这段 JavaScript 代码覆盖?

javascript - 使用AngularJS在Youtube API更新上出现400错误请求

javascript - 解决方法 AngularJS DOM 异常

linux - 如何让全局安装的节点模块在终端上运行