I am getting a 404 error on my new Reactjs app, is it looking for a main.js file?

会有一股神秘感。 提交于 2019-12-11 05:42:50

问题


This app should be rendering what I have here (/Users/ldco2016/Projects/reactquiz/src/components/App.jsx:

import React, {Component} from 'react';
import ReactDOM from 'react-dom';

class App extends Component{
    render(){
        return(
            <div>
                APP
            </div>
        )
    }
}

export default App

but instead I get a blank page and the Chrome Dev tools console is telling me this:

GET http://localhost:8080/app/js/main.js 404 (Not Found)

I know what that means but I am unclear as to whether it is trying to find the page I want to render or the main.js file.

/Users/ldco2016/Projects/reactquiz/package.json:

{
  "name": "reactquiz",
  "version": "1.0.0",
  "description": "A Reactjs Quiz App",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Daniel Cortes",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "6.13.*",
    "babel-loader": "6.2.*",
    "webpack": "1.13.*"
  },
  "dependencies": {
    "react": "^15.3.1",
    "react-dom": "^15.3.1"
  }
}

/Users/ldco2016/Projects/reactquiz/app/index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Reactjs Quiz</title>
</head>
<body>
    <div id="app"></div>

    <script src="js/main.js"></script>
</body>
</html>

ldco2016@DCortes-MacBook-Pro-3 ~/Projects/reactquiz/webpack.config.js:

module.export = {
    entry: [
        './src/index.js'
    ],
    output: {
        path: __dirname,
        filename: 'app/js/main.js'
    },
    module: {
        loaders: [{
            test: /\.jsx?$/,
            loader: 'babel',
            exclude: /node_modules/
        }]
    }

}

/Users/ldco2016/Projects/reactquiz/src/index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';

ReactDOM.render(
    <App />,
    document.getElementById('app')
);

回答1:


You need to run webpack to generate the main.js file. Make two changes to your file first in package.json add a script "start": "webpack --progress -p --config webpack.config.js --watch" and in your webpack.config.js change the loader to babel-loader.

P.S. Second change may not be required.

package.json

{
  "name": "reactquiz",
  "version": "1.0.0",
  "description": "A Reactjs Quiz App",
  "main": "index.js",
  "scripts": {
    "start": "webpack --progress -p --config webpack.config.js --watch"
  },
  "author": "Daniel Cortes",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "6.13.*",
    "babel-loader": "6.2.*",
    "webpack": "1.13.*"
  },
  "dependencies": {
    "react": "^15.3.1",
    "react-dom": "^15.3.1"
  }
}

webpack.config.js

module.export = {
    entry: [
        './src/index.js'
    ],
    output: {
        path: __dirname,
        filename: 'app/js/main.js'
    },
    module: {
        loaders: [{
            test: /\.jsx?$/,
            loader: 'babel-loader',
            exclude: /node_modules/
        }]
    }

}

Now run webpack with command

npm run start

now open your localhost and the above should work.



来源:https://stackoverflow.com/questions/39070441/i-am-getting-a-404-error-on-my-new-reactjs-app-is-it-looking-for-a-main-js-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!