简体   繁体   English

本地主机节点服务器无法从Webpack加载bundle.js

[英]localhost node server can't load bundle.js from webpack

I'm learning react following Isomorphic App tutorial . 我正在按照同构应用程序教程学习反应。

Since i'm really new to react, I strictly followed the tutorial, writing every single script as described. 由于我真的很新,因此我严格按照本教程进行操作,并按照说明编写每个脚本。 When I launch the server with npm start , the browser on localhost:3000 doesn't render the page as expected, showing only the title without the input field and the button for adding a new todo. 当我使用npm start服务器npm startlocalhost:3000上的浏览器无法按预期方式呈现该页面,仅显示没有输入字段的标题和用于添加新待办事项的按钮。 Opening the Dev Console, comes out that bundle.js can't get loaded and i really can't understand why. 打开开发控制台,结果是bundle.js无法加载,我真的不明白为什么。

If I point the browser to localhost:3000/home , on the other hand, it renders the input field and the button, but again bundle.js is not loaded, and the "app" doesn't work (ie doesn't add a new todo). 另一方面,如果我将浏览器指向localhost:3000/home ,它将呈现输入字段和按钮,但同样不会加载bundle.js ,并且“ app”不起作用(即未添加一个新的待办事项)。

i'm new to react, webpack and node, i googled a lot but i really can't get it through, i don't know what's happening. 我是新来的反应者,webpack和node,我在Google上搜索了很多,但是我真的无法通过它,我不知道发生了什么。 package.json and webpack.config too have been created following the tutorial indications. package.jsonwebpack.config也已按照本教程的指示创建。

I'm banging my head against the wall since a few days, please ease my pain, i beg you :D 几天以来,我把头撞在墙上,请减轻我的痛苦,求你:D

here's my config: 这是我的配置:

package.json : package.json

{
  "name": "isomorphic-redux",
  "version": "1.0.0",
  "repository": "https://github.com/bananaoomarang/isomorphic-redux",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "NODE_PATH=$NODE_PATH:./shared node --harmony .",
    "dev": "npm run start & webpack-dev-server --progress --color",
    "build": "NODE_ENV=production webpack --progress --color -p --config webpack.prod.config.js"
  },
  "author": "Milo Mordaunt <milomord@gmail.com>",
  "license": "MIT",
  "dependencies": {
    "axios": "^0.7.0",
    "express": "^4.13.3",
    "history": "^1.9.1",
    "immutable": "^3.7.5",
    "object-assign": "^4.0.1",
    "react": "^0.14.0",
    "react-dom": "^0.14.0",
    "react-redux": "^4.0.0",
    "react-router": "1.0.0-rc3",
    "redux": "^3.0.0",
    "webpack": "^1.13.2"
  },
  "devDependencies": {
    "babel": "latest",
    "babel-eslint": "^4.1.2",
    "babel-loader": "^5.3.2",
    "eslint": "^1.4.3",
    "eslint-plugin-react": "^3.3.2",
    "react-hot-loader": "^1.3.0",
    "webpack": "^1.12.1",
    "webpack-dev-server": "^1.11.0"
  }
}

webpack.config

var path    = require('path');
var webpack = require('webpack');

module.exports = {
  entry:  [
    'webpack-dev-server/client?http://127.0.0.1:8080/',
    'webpack/hot/only-dev-server',
    './client'
  ],
  output: {
    path:     path.join(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  resolve: {
    modulesDirectories: ['node_modules', 'shared'],
    extensions:         ['', '.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test:    /\.jsx?$/,
        exclude: /node_modules/,
        loaders: ['react-hot', 'babel']
      }
    ]
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin()
  ],
  devtool: 'inline-source-map',
  devServer: {
    hot: true,
    proxy: {
      '*': 'http://127.0.0.1:' + (process.env.PORT || 3000)
    },
    host: '127.0.0.1'
  }
};

the project structure is the following: 项目结构如下:

client/
---index.jsx
shared/
---actions/
---components/
------...
------Home.jsx
------index.jsx
---reducers/
---routes.jsx

index.js
server.jsx
package.json
webpack.config
.babelrc

You are running two somehow different setups that don't know of each other immediately. 您正在运行两个彼此不认识的不一样的设置。 When you do npm start you fire up the node process with your index.js file that in the end serves some html referencing a non-existing bundle.js file. 当您执行npm start时,将使用index.js文件npm start节点进程,该文件最后会提供一些HTML,这些HTML引用不存在的bundle.js文件。

On the other hand, when you run the webpack-dev-server , all bundled content will reside only in-memory and will never be written to your disk. 另一方面,当您运行webpack-dev-server ,所有捆绑的内容将仅驻留在内存中,并且永远不会写入磁盘。 so the file will not be available to the server started with npm start . 因此,以npm start的服务器将无法使用该文件。

But as you can see in the webpack config, it contains a section where a proxy is configured to route all unknown paths to a backend on port 3000. This is where the two setups are connected. 但是,正如您在webpack配置中看到的那样,它包含一个部分,其中将proxy配置为将所有未知路径路由到端口3000上的后端。这是两个设置之间的连接。 You need to run the webpack-dev-server using npm run dev and then open localhost:8080 in your browser. 您需要使用npm run dev运行webpack-dev-server,然后在浏览器中打开localhost:8080 This task automatically runs npm start as first command (which runs your node app) and afterwards starts the webpack-dev-server . 此任务自动以第一个命令的身份运行npm start (运行您的节点应用程序),然后启动webpack-dev-server So if you want to access your application, you must not use port 3000 but either go to 8080 which is the port of the webpack-dev-server . 因此,如果要访问您的应用程序,则必须不要使用端口3000而必须转到webpack-dev-server的端口8080

The error you get is also documented in the blog post at this position: https://medium.com/front-end-developers/handcrafting-an-isomorphic-redux-application-with-love-40ada4468af4#4c7c 您收到的错误也记录在此位置的博客文章中: https : //medium.com/front-end-developers/handcrafting-an-isomorphic-redux-application-with-love-40ada4468af4#4c7c

If you see a errors in the console about bundle.js not being found, Don't Panic. 如果您在控制台中看到关于找不到bundle.js的错误,请不要惊慌。 This is because the client is trying to load bundle.js, but we haven't configured Webpack's entry point yet, so we get nothing. 这是因为客户端正在尝试加载bundle.js,但我们尚未配置Webpack的入口点,因此一无所获。

And the solution can be found some lines later ( https://medium.com/front-end-developers/handcrafting-an-isomorphic-redux-application-with-love-40ada4468af4#0eed ): 稍后可以找到解决方案( https://medium.com/front-end-developers/handcrafting-an-isomorphic-redux-application-with-love-40ada4468af4#0eed ):

Now we can start our app with npm run dev (instead of npm start) and Webpack will serve our client a bundle.js, so it can do it's own routing from here-on-out. 现在,我们可以使用npm run dev(而不是npm start)启动我们的应用程序,并且Webpack将为我们的客户提供bundle.js,因此它可以从头到尾进行自己的路由。 It still won't look very interesting, but visiting http://localhost:8080/ should work without errors. 它看起来仍然不会很有趣,但是访问http:// localhost:8080 /应该可以正常工作。

According to your package.json you are using the new babel 6 release. 根据您的package.json您正在使用新的babel 6版本。 However you don't have all the required dependencies for this new release. 但是,您没有此新版本的所有必需依赖项。 According to babel-loader you need to install babel-preset-es2015 too: 根据babel-loader,您还需要安装babel-preset-es2015

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

As you are also using React, you need the react preset to be installed too. 当您还使用React时,还需要安装react预设。 So install both: 因此,请同时安装:

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

Create a file in your package folder called .babelrc to keep the babel configuration and enable both presets: 在软件包文件夹中创建一个名为.babelrc的文件,以保持babel的配置并启用两个预设:

.babelrc
{
  "presets": ["es2015", "react"]
}

And then run the server again: 然后再次运行服务器:

npm run dev

Then http://localhost:8090/assets/bundle.js should show the bundled module. 然后http://localhost:8090/assets/bundle.js应该显示捆绑的模块。

My diagnosis: 我的诊断:

You cannot get the the bundle.js because your npm run dev throws some errors trying to apply the babel loader because it is not properly configured. 您无法获取bundle.js因为您的npm run dev由于未正确配置babel加载程序而引发一些错误。 Then, when you try to request the bundle.js file you get a 404 error because it has not been generated. 然后,当您尝试请求bundle.js文件时,会收到404错误,因为它尚未生成。

From -> https://stackoverflow.com/questions/33502987/webpack-bundle-js-not-found 从-> https://stackoverflow.com/questions/33502987/webpack-bundle-js-not-found

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM