缺少来自 webpack 的输出文件和 node.js
Missing output file from webpack and node.js
我正在尝试设置 node.js 服务器。当前,当我 运行 npm 以当前 webpack.config.js 启动时,一切都会加载。当我 运行 节点服务器时,index.js 丢失。
webpack.config.js
var config = {
context: path.join(__dirname, 'src'),
entry: './js/main.js',
output: {
path: __dirname + '/src/',
filename: 'index.js',
},
devServer: {
inline: true,
port: 8080
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
}
}
module.exports = config;
这里是html,省略了不需要看的东西。
<html lang="en">
<head>
</head>
<body>
<div id="app"></div>
<script src="index.js"></script>
</body>
</html>
Package.json
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"start": "webpack-dev-server --content-base src --hot",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"history": "^1.17.0",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-router": "^4.2.0",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.3"
},
"devDependencies": {
"express": "^4.16.2",
"http": "0.0.0",
"react-socket-io": "^0.2.4",
"socket.io": "^2.0.3",
"webpack": "^3.8.1"
}
}
我是 node.js 和 webpack 的新手,所以我不确定为什么 index.js 在 运行 npm start 时输出,但在 运行 节点服务器。我可以上传 server.js,但它遵循一个非常基本的模板。
编辑:看来我成功了,我需要 运行 webpack 开发服务器以及不同端口上的 node.js 服务器。跟进问题,是否可以将其更改为仅 运行 节点服务器并且一切正常?
在启动节点服务器之前,您需要运行 webpack --config path/to/webpack.config.js
在文件系统中生成index.js
。
您可以将 build
命令添加到您的 package.json
:
{
...
"scripts": {
"start": "webpack-dev-server --content-base src --hot",
"build": "webpack --config webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
...
}
然后在启动节点服务器之前运行npm run build
我正在尝试设置 node.js 服务器。当前,当我 运行 npm 以当前 webpack.config.js 启动时,一切都会加载。当我 运行 节点服务器时,index.js 丢失。
webpack.config.js
var config = {
context: path.join(__dirname, 'src'),
entry: './js/main.js',
output: {
path: __dirname + '/src/',
filename: 'index.js',
},
devServer: {
inline: true,
port: 8080
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
}
}
module.exports = config;
这里是html,省略了不需要看的东西。
<html lang="en">
<head>
</head>
<body>
<div id="app"></div>
<script src="index.js"></script>
</body>
</html>
Package.json
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"start": "webpack-dev-server --content-base src --hot",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"history": "^1.17.0",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-router": "^4.2.0",
"webpack": "^3.8.1",
"webpack-dev-server": "^2.9.3"
},
"devDependencies": {
"express": "^4.16.2",
"http": "0.0.0",
"react-socket-io": "^0.2.4",
"socket.io": "^2.0.3",
"webpack": "^3.8.1"
}
}
我是 node.js 和 webpack 的新手,所以我不确定为什么 index.js 在 运行 npm start 时输出,但在 运行 节点服务器。我可以上传 server.js,但它遵循一个非常基本的模板。
编辑:看来我成功了,我需要 运行 webpack 开发服务器以及不同端口上的 node.js 服务器。跟进问题,是否可以将其更改为仅 运行 节点服务器并且一切正常?
在启动节点服务器之前,您需要运行 webpack --config path/to/webpack.config.js
在文件系统中生成index.js
。
您可以将 build
命令添加到您的 package.json
:
{
...
"scripts": {
"start": "webpack-dev-server --content-base src --hot",
"build": "webpack --config webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
...
}
然后在启动节点服务器之前运行npm run build