转载

Webpack 常见问题与解答

1.同级目录的文件引用需要带上 './'

[Q] Module not found: Errorr: Cannot resolve module

ERROR in ./src/js/modules/header/index.jsx

Module not found: Error: Cannot resolve module 'index.less' in C:/Users/Desktop/src/js/modules/header

@ ./src/js/modules/header/index.jsx 5:0-21

[A] 将 require('index.less'); 替换为 require('./index.less');

2.window 使用 / 作为路径分隔符

[Q] Module not found: Error: Cannot resolve 'file' or 'directory' ./../../../../node_modules/css-loader/index.js and Module not found: Error: Cannot resolve 'file' or 'directory' ./../../../../node_modules/style-loader/addStyles.js

ERROR in ./src/js/modules/header/index.less

Module not found: Error: Cannot resolve 'file' or 'directory' ./../../../../node_modules/css-loader/index.js in C:/Users/Desktop/src/js/modules/header

@ ./src/js/modules/header/index.less 4:14-134

ERROR in ./src/js/modules/header/index.less

Module not found: Error: Cannot resolve 'file' or 'directory' ./../../../../node_modules/style-loader/addStyles.js in C:/Users/Desktop/src/js/modules/header

@ ./src/js/modules/header/index.less 7:13-77

[A] webpack.config.js 中,路径的使用 path.join(__dirname, 'src'),而不使用 __dirname + '/src',eg:

var path = require('path'); module.exports = {     context: path.join(__dirname, 'src'),     resolve: {         root: path.resolve(__dirname, "src"),         // root: __dirname + '/src',     } }

两者的不同,可看一下输出

console.log(path.resolve(__dirname, "src")); // 输出: C:/Users/joeyguo/Desktop/src console.log(__dirname + '/src'); // 输出: C:/Users/joeyguo/Desktop/src 

3.less-loader 的使用需要配套 less module

[Q] ERROR in Cannot find module 'less'

ERROR in Cannot find module 'less'

@ ./src/js/modules/header/index.less 4:14-134

[A] install less module

npm install less less-loader --save-dev 

未完,持续更新,欢迎讨论与补充~

原文  https://github.com/joeyguo/blog/issues/7
正文到此结束
Loading...