我刚接触webpack并做出反应,只是通过文档并创建了一个工作示例。不幸的是我卡住了,无法继续。问题是没有生成捆绑文件。 我创建的文件是
的package.json
{
"name": "rohith",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.4.1",
"react-dom": "^15.4.1",
"webpack": "^1.13.3",
"webpack-dev-server": "^1.16.2"
}
}
webpack.config.js
module.export = {
entry : './main.js',
output : {
path : './',
filename : 'index.js'
},
devServer : {
inline : true,
port : 3333
},
module : {
loaders : [
{
test : /\.js$/,
exclude : /node_modules/,
loader : 'babel',
query : {
presets : ['es2015','react']
}
}
]
}
}
App.js
import React from 'react';
class App extends React.Component {
render(){
return <div>Hello</div>
}
}
export default App
main.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />,document.getElementById('app'));
的index.html
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>Setup</title>
</head>
<body>
<div id = "app"></div>
<script src ="index.js"></script>
</body>
</html>
我得到的bundle是有效的,但是没有生成index.js。 无法在localhost 3333中运行
谢谢,
答案 0 :(得分:8)
我认为问题在于你没有给出绝对输出路径。
试试这个:
var path = require('path');
module.exports = {
entry : './main.js',
output : {
path : path.join(__dirname, './'),
filename : 'index.js'
},
devServer : {
inline : true,
port : 3333
},
module : {
loaders : [
{
test : /\.js$/,
exclude : /node_modules/,
loader : 'babel',
query : {
presets : ['es2015','react']
}
}
]
}
}
希望有所帮助:)
答案 1 :(得分:3)
在webpack.config.js中,使用module.exports
代替module.export
。见Output filename not configured Error in Webpack
另请注意,您的package.json
缺少某些依赖项。以下是更新后的package.json
:
{
"name": "rohith",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.4.1",
"react-dom": "^15.4.1",
"webpack": "^1.13.3",
"webpack-dev-server": "^1.16.2"
},
"devDependencies": {
"babel": "^6.5.2",
"babel-core": "^6.18.2",
"babel-loader": "^6.2.8",
"babel-preset-es2015": "^6.18.0",
"babel-preset-react": "^6.16.0"
}
}
答案 2 :(得分:3)
用以下内容替换脚本对象:
"scripts": {
"start": "npm run build",
"build": "webpack -p && webpack-dev-server"
},
然后,运行$ npm start
答案 3 :(得分:2)
对我来说,问题在于&#34;脚本&#34;
下的package.json以下是修复:
在package.json文件中,在脚本下添加以下内容:
&#34;脚本&#34;:{
&#34;开始&#34;:&#34; webpack-dev-server&#34;,
&#34; build&#34;:&#34; webpack -p&#34; }
首先我运行构建然后开始。
纱线构建
如果您第一次构建,那么将创建您的bundle.js文件,之后您可以使用此命令:
纱线开始
答案 4 :(得分:0)
如果没有输出文件(bundle.js)且构建成功
Hash: 77a20ba03ab962a1f5be
Version: webpack 4.41.0
Time: 1039ms
Built at: 10/04/2019 5:24:48 PM
Asset Size Chunks Chunk Names
main.js 1.09 MiB main [emitted] main
Entrypoint main = main.js
[./react_rest/frontend/src/index.js] 35 bytes {main} [built]
+ 12 hidden modules
Webpack :
module.exports = {
entry: './react_rest/frontend/src/index.js',
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}]
},
output: {
filename: 'main.js'
}
};
尝试使用var path = require('path');
并分配输出目录
var path = require('path');
module.exports = {
entry: './react_rest/frontend/src/index.js',
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}]
},
output: {
path: path.join(__dirname,'./react_rest/frontend/static/frontend/'),
filename: 'main.js'
}
};