javascript Webpack console.log 输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48322931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Webpack console.log output?
提问by Samuel Gosling
Can anyone direct me in the right direction?
任何人都可以指导我朝着正确的方向前进吗?
So i've setup the webpack-dev-server with the truffle suite demo, just to get a basis on the foundation of my app. So my config file includes index.html & app.js, yet it try to display a console.log output to from app.js nothing shows via the console?
所以我用 truffle 套件演示设置了 webpack-dev-server,只是为了在我的应用程序的基础上获得一个基础。所以我的配置文件包括 index.html 和 app.js,但它尝试显示来自 app.js 的 console.log 输出,但控制台没有显示任何内容?
webpack.config.js
webpack.config.js
const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports =
{
entry: './app/javascripts/app.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'app.js',
},
plugins: [
// Copy our app's index.html to the build folder.
new CopyWebpackPlugin([
{ from: './app/index.html', to: "index.html" }
])
],
module: {
rules: [
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
],
loaders: [
{ test: /\.json$/, use: 'json-loader' },
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['es2015'],
plugins: ['transform-runtime']
}
}
]
},
devServer: {
compress: true,
disableHostCheck: true, // That solved .
quiet: false,
noInfo: false,
stats: {
// Config for minimal console.log mess.
colors: true,
version: false,
hash: false,
timings: false,
chunks: false,
chunkModules: false
}
}
}
app.js
应用程序.js
// Import libraries we need.
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'
// Import our contract artifacts and turn them into usable abstractions.
import metacoin_artifacts from '../../build/contracts/MetaCoin.json'
import dextre_artifacts from '../../build/contracts/Dextre.json'
console.log("starting!");
Output when running webpack
运行 webpack 时的输出
Project is running at http://localhost:8080/
webpack output is served from /
Asset Size Chunks Chunk Names
app.js 1.93 MB 0 [emitted] [big] main
index.html 19.8 kB [emitted]
webpack: Compiled successfully.
Where can view the "starting!" output, this is a real annoyance as i need to tackle errors. I've tried viewing at http://localhost:8080//and http://localhost:8080/webpack-dev-server//, but no luck.
哪里可以查看“出发!” 输出,这是一个真正的烦恼,因为我需要解决错误。我试过在http://localhost:8080//和http://localhost:8080/webpack-dev-server// 上查看,但没有运气。
回答by nupanick
I had this same problem. As far as I can tell, the problem is that Webpack does not actually run the generated code on the server. The process that runs on the server simply checks for file changes and re-compiles when necessary. The code is actually all running on the client. So, the only way to view output from console.log() is to view the client browser's console.
我有同样的问题。据我所知,问题在于 Webpack 实际上并没有在服务器上运行生成的代码。在服务器上运行的进程只是检查文件更改并在必要时重新编译。代码实际上都在客户端上运行。因此,查看 console.log() 输出的唯一方法是查看客户端浏览器的控制台。
Part of the confusion here is that while normally, node runs javascript on the server, in this case, node is delegating to a separate program entirely. You can run this whole thing without node installed at all, just using a standalone installation of the webpack executable. Node's execution environment is never used, thus you can't log to it.
这里的部分混乱是,虽然通常情况下,节点在服务器上运行 javascript,但在这种情况下,节点完全委托给一个单独的程序。你可以在根本不安装 node 的情况下运行这整个事情,只需使用 webpack 可执行文件的独立安装。Node 的执行环境从未使用过,因此您无法登录到它。

