我是webpack的新手,我想在我的项目中使用第三方反应组件。我安装了我需要的组件,并创建了目录node_modules
,我的项目树看起来像:
reactcalendar
|--node_modules
| |--.bin
| |--babel-cli
| |--babel-core
| |--babel-preset-es2015
| |--babel-preset-react
| |--babelify
| |--file-loader
| |--moment
| |--react
| |--react-big-calendar (the third party component)
| |--react-dom
| |--webpack
|--.babelrc
|--bundle.js (empty)
|--index.html
|--index.js
|--package.json
|--webpack.config.js
某些文件用于浏览器化,但它给了我同样的错误...
我的 index.html 如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>React Calendar</title>
<script src="bundle.js" type="text/javascript"></script>
</head>
<body>
<div id="content"></div>
</body>
</html>
我的 index.js 如下所示:
import BigCalendar from 'react-big-calendar';
import moment from 'moment';
BigCalendar.momentLocalizer(moment);
ReactDOM.render(<BigCalendar
events={myEventsList}
startAccessor='startDate'
endAccessor='endDate'
/>, document.getElementById('content'));
我的webpack.config.js
看起来像这样:
module.exports = {
context: __dirname,
output: {
filename: "bundle.js",
path: __dirname
},
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loaders: ["babel-loader"]
},
{
test: /\.html$/,
loader: "file?name=[name].[ext]",
}
]
},
entry: {
javascript: "./index.js",
html: "./index.html"
}
}
当我将命令webpack
运行到我的根项目中时,它给了我这个错误:
C:\Users\ernest\PhpstormProjects\reactcalendar>webpack
Hash: ebfe4ff0eeeaed3060c6
Version: webpack 1.13.1
Time: 12753ms
Asset Size Chunks Chunk Names
index.html 231 bytes [emitted]
bundle.js 469 kB 0, 1 [emitted] html, javascript
+ 105 hidden modules
ERROR in ./index.js
Module not found: Error: Cannot resolve module 'react-big-calendar' in C:\Users\ernest\PhpstormProjects\reactcalendar
@ ./index.js 3:24-53
也许我对webpack的配置文件有误?
答案 0 :(得分:2)
1.删除node_modules文件夹。
2.then npm install
3.将index.js中的代码更改为此
import {render} from 'react-dom';
import React from 'react';
import BigCalendar from 'react-big-calendar';
import moment from 'moment';
BigCalendar.setLocalizer(BigCalendar.momentLocalizer(moment));
const myEventsList = [{'event':'test2'},{'event':'test1'},{'event':'test3'}]
render(<BigCalendar
events={myEventsList}
startAccessor='startDate'
endAccessor='endDate'
/>, document.getElementById('content'));
4.in index.html你应该将bundle.js放入body
<body>
<div id="content"></div>
<script src="bundle.js" type="text/javascript"></script>
</body>
答案 1 :(得分:1)
我没有使用React,但是我的问题通过将module
中tsconfig.json
的{{1}}字段设置为UMD
来解决。
AMD