创建一个反应项目

时间:2018-10-15 05:51:41

标签: javascript json reactjs visual-studio project

prtScn 我正在尝试启动新的React应用,但是我遇到了问题。 我使用visual studio代码,并按照说明进行操作: 在可视代码终端中:

create-react-app sali (sali is the name)
npm install 
 node node_modules\webpack\bin\webpack.js -d -w

然后我收到此消息:

ERROR in ./src/index.js 7:16
Module parse failed: Unexpected token (7:16)
You may need an appropriate loader to handle this file type.
| import * as serviceWorker from './serviceWorker';
|
> ReactDOM.render(<App />, document.getElementById('root'));
|
| // If you want your app to work offline and load faster, you can change

这是代码:(这是create react app的默认代码) index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

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

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
serviceWorker.unregister();

app.js

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    );
  }
}

export default App;

package.json

{
  "name": "sali",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-scripts": "2.0.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "webpack-cli": "^3.1.2"
  }
}

拜托,怎么了?

1 个答案:

答案 0 :(得分:1)

我想你在终端上写的是

> create-react-app sali
> npm install 
> node node_modules\webpack\bin\webpack.js -d -w

对于Create-React-App,这不是您应该做的,您应该这样做:

> create-react-app sali
> cd sali
> npm run start

您应该阅读React documentation and tutorials,因为它们将对您有帮助!