【问题标题】:How to include a css/js file from a node package?如何从节点包中包含 css/js 文件?
【发布时间】:2019-03-17 21:50:42
【问题描述】:

我已经安装了npm package。现在我想通过键入以下内容在我的 html 页面中包含 css 和 js 文件:

      <link href="/root/node_modules/quill/dist/quill.snow.css" rel="stylesheet">

但上面的路径不起作用。我的 html 页面位于 /var/www/html 这样做的正确方法是什么。这可能是重复的,但我无法找到答案。

【问题讨论】:

  • 您可以编辑您的帖子以提供文件结构(最好是文本)

标签: html css node.js npm


【解决方案1】:

您需要确保您的 CSS 和 JS 文件与您的 HTML 文件位于同一目录中(可能是 src/app)。

例如

src
 |
 | app
    |
    | css
    | js
   index.html

所以网页版中index.html的路径是/index.html,CSS文件夹的路径(相对于index.html)是./css

这是一个很棒的explanation of relative paths in node applications

在节点模块中使用 CSS 等第三方库时,您可以在样式表中使用它:

@import "~quill/dist/quill.snow";

您还需要require the module in your config file - see this discussion in GitHub for more info

【讨论】:

  • 所以我只是将完整的包目录复制到我的网站文件夹中?
  • 啊,如果它们是不同的库 CSS 文件
  • 对不起,我不明白。
  • 我的意思是如果它们是 npm 包的一部分,你应该从那里使用它们。只是想弄清楚你应该如何引用它们,我马上回来:) 你在使用 SCSS 吗?
  • 非常感谢您的帮助!
猜你喜欢
  • 2017-10-15
  • 2011-04-14
  • 2014-07-21
  • 1970-01-01
  • 2013-05-25
  • 2014-05-22
  • 1970-01-01
  • 2016-01-22
  • 2020-03-14
相关资源
最近更新 更多