错误:找不到或无法读取要导入的文件:~bootstrap/scss/bootstrap

新手上路,请多包涵

我按照 getbootstrap.com 上的说明进行操作,认为一切都会正常进行。它不是到目前为止:

一切似乎都很好,直到我尝试加载页面,此时我的 Express.js 应用程序抛出错误

    [[sass] error: File to import not found or unreadable: ~bootstrap/scss/bootstrap.
Parent style sheet: .../sass/app.scss at options.error (.../node-sass/lib/index.js:291:26)

我已经尝试过 npm install,重新启动我的服务器,查看 Google,StackOverflow(是的,我知道有很多类似的问题,但没有一个回答我的问题),Bootstrap 4 GitHub 问题页面,到目前为止我还没有能够想出答案。

会不会是我把依赖装错地方了? (开发而不是生产或相对)

为什么我会收到此错误??

我的 webpack.config.js 文件看起来像这样……

  module.exports = {
  entry: './src/index.js',
  output: {
    path: __dirname + '/public',
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.json$/,
        loader: 'json-loader'
      },
      {
        test: /\.js$/,
        loader: 'babel-loader'
      },
      {
        test: /\.(scss)$/,
        use: [{
          loader: 'style-loader', // inject CSS to page
        }, {
          loader: 'css-loader', // translate CSS into CommonJS modules
        }, {
          loader: 'postcss-loader', // run post CSS actions
          options: {
            plugins: function () { // post css plugins, can be exported to postcss.config.js
              return [
                require('precss'),
                require('autoprefixer')
              ];
            }
          }
        }, {
          loader: 'sass-loader' // compile Sass to CSS
        }]
      }
    ]
  }
};

我的 package.json 文件

...
"scripts": {
    "start": "nodemon --exec babel-node server.js --ignore public/",
    "dev": "webpack -wd",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
"dependencies": {
    "axios": "^0.17.1",
    "bootstrap": "^4.0.0",
    "ejs": "^2.5.7",
    "express": "^4.16.2",
    "jquery": "^3.3.1",
    "mongoose": "^5.0.0",
    "node-sass-middleware": "^0.11.0",
    "popper.js": "^1.12.9",
    "precss": "^3.1.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
  },
  "devDependencies": {
    "autoprefixer": "^7.2.5",
    "babel-cli": "^6.26.0",
    "babel-eslint": "^8.2.1",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",
    "css-loader": "^0.28.9",
    "eslint": "^4.15.0",
    "eslint-plugin-react": "^7.5.1",
    "node-sass": "^4.7.2",
    "nodemon": "^1.14.11",
    "postcss-loader": "^2.0.10",
    "sass-loader": "^6.0.6",
    "style-loader": "^0.19.1",
    "webpack": "^3.10.0"
  }
}

postcss.config.js

 module.exports = {
  plugins: [
    require('autoprefixer')
  ]
};

在 app.scss 里面我有

@import "custom";
@import "~bootstrap/scss/bootstrap";

原文由 quarterpi 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 931
2 个回答

当 Sass 由它自己的 CLI 预编译时,它自己处理 @imports ,因此有时不理解 ~ 符号。因此,您可以首先导入 "node_modules/bootstrap/scss/bootstrap"; 并将 ~ node_modules/

原文由 Ram Chandra Neupane 发布,翻译遵循 CC BY-SA 4.0 许可协议

我有一个类似的错误

要导入的文件未找到或不可读:node_modules/bootstrap-sass/assets/stylesheets/bootstrap

只需将“bootstrap-sass”:“^3.3.7”添加到你的 package.json 中的 devDependencies,然后在你的项目目录中运行 npm update、npm install。

原文由 Max Sherbakov 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题