我正在将样式加载器与webpack和react框架一起使用。当我在终端中运行webpack时,Module not found: Error: Cannot resolve module 'style-loader'尽管我已正确指定了文件路径,但我仍获取import.js文件。
Module not found: Error: Cannot resolve module 'style-loader'
import '../css/style.css'; import React from 'react'; import ReactDOM from 'react-dom'; import jQuery from 'jquery'; import TopicsList from '../components/topic-list.jsx'; import Layout from '../components/layout.jsx';
webpack.config.js:
var webpack = require('webpack'); var path = require('path'); var BUILD_DIR = path.resolve(__dirname, 'build'); var APP_DIR = path.resolve(__dirname, 'build'); module.exports = { entry: [ // Set up an ES6-ish environment 'babel-polyfill', // Add your application's scripts below APP_DIR + '/import.js' ], output: { path: BUILD_DIR, filename: 'bundle.js' }, module: { loaders: [ { test: /\.jsx?$/, loader: 'babel', exclude: /node_modules/, query: { plugins: ['transform-runtime'], presets: ['es2015', 'stage-0', 'react'] } }, { test: /\.css$/, loader: "style-loader!css-loader" } ], resolve: { extensions: ['', '.js', '.jsx', '.css'] } } };
尝试在下面运行脚本:
npm install style-loader --save
修改webpack配置,在中添加modulesDirectories字段resolve。
modulesDirectories
resolve
resolve: { extensions: ['', '.js', '.jsx', '.css'], modulesDirectories: [ 'node_modules' ] }