css - 无法使用 Node-sass、bootstrap-sass 加载 Bootstrap

标签 css node.js twitter-bootstrap

我在一个基于 node/express 的旧/新元素中,其中 boostrap 源被痛苦地复制粘贴(和黑客攻击)。 我想使用 bootstrap 包更易于维护。但是,在添加 bootstrap-sass 和 node-sass 之后,我遇到了这个错误:

这里是错误

in /vagrant/sass/theme.scss:28:1
Error: File to import not found or unreadable: bootstrap/variables
Parent style sheet: /vagrant/sass/theme.scss
    at options.error (/vagrant/node_modules/node-sass/lib/index.js:277:32)
  source: /vagrant/sass/vendor/bootstrap-datepicker/css/bootstrap-datepicker3.min.scss
  dest: /vagrant/public/css/vendor/bootstrap-datepicker/css/bootstrap-datepicker3.min.css
  read: /vagrant/public/css/vendor/bootstrap-datepicker/css/bootstrap-datepicker3.min.css
  source: /vagrant/sass/theme.scss
  dest: /vagrant/public/css/theme.css
  read: /vagrant/public/css/theme.css
  error: File to import not found or unreadable: bootstrap/variables
Parent style sheet: /vagrant/sass/theme.scss

in /vagrant/sass/theme.scss:28:1
Error: File to import not found or unreadable: bootstrap/variables
Parent style sheet: /vagrant/sass/theme.scss
    at options.error (/vagrant/node_modules/node-sass/lib/index.js:277:32)

服务器.js

app.use(
  sass({
    src: __dirname + '/sass',
    dest: __dirname + '/public/css',
    prefix: '/css',
    // outputStyle: 'compressed',
    debug: true,
  })
);

package.json:

"dependencies": {
  "bcrypt-nodejs": "0.0.3",
  "body-parser": "^1.13.1",
  "bootstrap-sass": "^3.3.6",
  "connect-flash": "^0.1.1",
  "cookie-parser": "^1.3.5",
  "express": "^4.12.4",
  "express-session": "^1.11.3",
  "mysql": "^2.7.0",
  "node-sass": "^3.4.2",
  "node-sass-middleware": "^0.9.7",
  "passport": "^0.2.2",
  "passport-facebook": "^2.0.0",
  "passport-google-oauth": "^0.2.0",
  "passport-local": "^1.0.0",
  "passport-twitter": "^1.0.3",
  "randomstring": "^1.1.3",
  "sendgrid": "^1.9.2",
  "sequelize": "^3.9.0",
  "sequelize-fixtures": "^0.4.8",
  "shelljs": "^0.5.3",
  "swig": "^1.4.2"
},
"devDependencies": {
  "eslint": "^1.10.3",
  "gulp": "^3.9.0",
  "gulp-zip": "^3.0.2",
  "jake": "^8.0.12",
  "minimist": "^1.2.0",
  "node-sass": "^3.3.3",
  "node-sass-middleware": "^0.9.6"
}

主题.scss

/*!
 * Bootstrap v3.3.6 (http://getbootstrap.com)
 * Copyright 2011-2015 Twitter, Inc.
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 */

// Core variables and mixins
@import "bootstrap/variables";
@import "bootstrap/mixins";

// Reset and dependencies
@import "bootstrap/normalize";
@import "bootstrap/print";
@import "bootstrap/glyphicons";

// Core CSS
@import "bootstrap/scaffolding";
@import "bootstrap/type";
@import "bootstrap/code";
@import "bootstrap/grid";
@import "bootstrap/tables";
@import "bootstrap/forms";
@import "bootstrap/buttons";

// Components
@import "bootstrap/component-animations";
@import "bootstrap/dropdowns";
@import "bootstrap/button-groups";
@import "bootstrap/input-groups";
@import "bootstrap/navs";
@import "bootstrap/navbar";
@import "bootstrap/breadcrumbs";
@import "bootstrap/pagination";
@import "bootstrap/pager";
@import "bootstrap/labels";
@import "bootstrap/badges";
@import "bootstrap/jumbotron";
@import "bootstrap/thumbnails";
@import "bootstrap/alerts";
@import "bootstrap/progress-bars";
@import "bootstrap/media";
@import "bootstrap/list-group";
@import "bootstrap/panels";
@import "bootstrap/responsive-embed";
@import "bootstrap/wells";
@import "bootstrap/close";

// Components w/ JavaScript
@import "bootstrap/modals";
@import "bootstrap/tooltip";
@import "bootstrap/popovers";
@import "bootstrap/carousel";

// Utility classes
@import "bootstrap/utilities";
@import "bootstrap/responsive-utilities";

我是 node 的新手,来自 Rails,在 Rails 中使用 sass 设置 Bootstrap 非常简单。如果您需要有关该元素的其他文件/信息,请告诉我。

谢谢你的帮助!

最佳答案

我在将 bootstrap-sass 导入 React/Webpack 元素时遇到了类似的问题,但解决方案是相同的。在您的主 scss 文件 (theme.scss) 中,您需要通过 node_modules 包内的相对路径减去 node_modules 路径段来 @import 各个 mixin。

如果您正在提取单个功能,那么您需要更改 @import "bootstrap/feature" 路径以反射(reflect)您要在 bootstrap-sass 包中使用的每个功能的路径。还要注意路径中前面的 ~,它解决了导航到 SCSS 文件的错误。

@import "~bootstrap-sass/assets/stylesheets/bootstrap/variables";

另一个陷阱,如果你要拉入字形,你需要将 $icon-font-path 设置为指向包中的字体文件夹,否则服务器会出错出去寻找那些文件。

$icon-font-path: "~bootstrap-sass/assets/fonts/bootstrap/";
@import "bootstrap-sass/assets/stylesheets/bootstrap/glyphicons";

如果您只想使用包中的所有内容,您只需设置图标字体路径并在 theme.scss 文件中引入所有样式即可。

// Bootstrap
$icon-font-path: "~bootstrap-sass/assets/fonts/bootstrap/";
@import "~bootstrap-sass/assets/stylesheets/_bootstrap.scss";

关于css - 无法使用 Node-sass、bootstrap-sass 加载 Bootstrap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34788771/

相关文章:

html - 使 CSS 下拉内容向右对齐而不是向左对齐

javascript - Chrome 和 Firefox 忽略顶部和左侧的 Javascript 样式

html - 将两个链接移动到导航右侧

node.js - 在 Angular CLI 应用程序中安装 express 时出现 "ENOTEMPTY: directory not empty, rmdir ....."错误。

javascript - 如何分离 session 控制而不是将其写在每个路由中

javascript - 使用 Node 创建的图像响应 GET 请求

javascript - 如何使移动设备的导航栏菜单占据我设备屏幕的 100%

ruby-on-rails - "bootstrap-sass"不适用于 rails 3.2.2

html - 无法从 div 顶部删除空间

css - 如何禁用链接的 Bootstrap 悬停颜色?