安装axios后出现“找不到模块” babel错误

时间:2019-01-15 19:44:48

标签: javascript node.js reactjs axios babeljs

我使用create-react-app来建立一个React项目,然后在同一项目文件夹中安装axios

App.js中,我导入并使用了axios

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

class App extends Component {
  state = {users: []}

  componentDidMount() {
    axios.get('/users')
      .then(res => res.json())
      .then(users => this.setState({ users }));
  }

  render() {
    return (
      <div className="App">
        <h1>Users</h1>
        {this.state.users.map(user =>
          <div key={user.id}>{user.username}</div>
        )}
      </div>
    );
  }
}

export default App;

,并出现以下错误:

./node_modules/axios/index.js
Module not found: Can't resolve '/Users/katietruong/Projects/read- 
it/client/node_modules/react-scripts/node_modules/babel- 
loader/lib/index.js' in '/Users/katietruong/Projects/read-it/client' 
.  

它如何发生以及如何解决?

1 个答案:

答案 0 :(得分:0)

确保您具有最新版本的npm。

npm install npm@latest -g

如果仍然无法解决问题,您可以尝试删除node_modules并再次运行npm install

rm -Rf node_modules
npm install