Webpack+React项目入门——入门及配置Webpack

### 一、配置过程中遇到的问题

1. Webpack版本问题

终端输入:npm run build

error1:
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.- configuration.module has an unknown property 'loaders'. These properties are valid:object { exprContextCritical?, exprContextRecursive?, exprContextRegExp?, exprContextRequest?, noParse?, rules?, defaultRules?, unknownContextCritical?, unknownContextRecursive?, unknownContextRegExp?, unknownContextRequest?, unsafeCache?, wrappedContextCritical?, wrappedContextRecursive?, wrappedContextRegExp?, strictExportPresence?, strictThisContextOnImports? }
-> Options affecting the normal modules (NormalModuleFactory).

原因:webpack版本问题,无法解析'loaders'

解决:安装webpack的其他版本:

npm install [email protected] --save-dev
或者
npm install [email protected] --save-dev

error2:
Module build failed: TypeError: fileSystem.statSync is not a function

原因:webpack版本不一致导致
解决:

npm install [email protected] --save-dev

2. 模块安装问题

error1:
Module build failed: Error: Couldn't find preset "env" relative to directory

原因:配置的env需要babel-preset-env模块
解决:

npm install babel-preset-env --save-dev

error2:
Module build failed: Error: Illegal import declation

原因:使用babel解析es6失败
解决:

npm install babel-preset-es2015 --save-dev

3. 其他

error:
React+Webpack,打包成功后,node启动服务.无法访问编译后的文件。
打包后的index.html可以访问,但是bundle.js无法访问.本地直接打开可正常引入index.html和bundle.js.但开启服务访问,无法成功获得js.

原因:node配置静态文件目录指向错误. html文件引入目录配置正确,但js等静态文件引入目录配置错误

解决:

//引入html文件的目录指向
app.engine('html', ejs.renderFile);
app.set('views', path.join(__dirname, './client/dist'));    //目录
app.set('view engine', 'html');

//引入css、js、img....等静态文件目录指向
app.use(express.static(path.join(__dirname, './client/dist')));

猜你喜欢

转载自www.cnblogs.com/soyxiaobi/p/9451144.html