Search code examples
javascriptnode.jsreactjsmongodbmern

Why can't React find this file?


React can't find this file that I'm trying to import.

I tried every different version of changing the filepath in the import I could, added/removed .js from all of the attempts, triple checked that there were no spelling errors/capitalization problems, and did an npm update just to try something different. I've hit a wall here.

The main errors I get are:

[Error: ENOENT: no such file or directory, stat '/initrd.img'] { errno: -2, code: 'ENOENT', syscall: 'stat', path: '/initrd.img' }

./src/App.js Module not found: You attempted to import /components/number.js which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.

I'm thinking it's a problem with how I'm trying to import/export things maybe? Here's the resources I've used to hack together what I have so far:

mongoDB MERN tutorial

geeksforgeeks tutorial

App.js

import logo from './logo.svg';
import './App.css';
import React, { Component } from 'react';
import { Route } from 'react-router-dom';
import Numbers from './components/number.js'; 

class App extends Component {
  constructor(props) {
    super(props)

    this.state = {
      buttonText: "button1"
    }

    this.changeWord = this.changeWord.bind(this);
  }

  changeWord() {
    if (this.state.buttonText === "button2") {
      this.setState({
        buttonText: "button1"
      })
    }
    else {
      this.setState({
        buttonText: "button2"
      })
    }
  }

  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>
        <Numbers />
        <button className="test-button" onClick={this.changeWord}>{this.state.buttonText}</button>
      </header>
    </div>
  );
  }
}

export default App;

number.js

import React, { Component } from "react";
import axios from "axios";
import { response } from "express";

export default class Numbers extends Component {
    constructor(props) {
        super(props);

        this.state = {
            numbers: []
        };
    }

    componentDidMount() {
        axios
        .get("http://localhost:3000/record/")
        .then((response) => {
            this.setState({
                numbers: response.data
            });

            console.log(this.state.numbers);
        })
        .catch(function(err) {
            console.log(err);
        });
    }

    render() {
        return (
            <div>
                <h3>Numbers</h3>
                <table>
                    <thead>
                        <tr>
                            <th>Number</th>
                        </tr>
                    </thead>
                    <tbody>{this.numbers()}</tbody>
                </table>
            </div>
        );
    }
}

Solution

  • I figured it out.

    I recently updated my machine using sudo apt-get update && sudo apt-get upgrade, which for some reason seems to have broken the links to /initrd.img and /initrd.img.old in the root file path(/).

    I ran sudo update-grub, which fixed that issue. Afterwards, I noticed that quickly before it (still) couldn't find /initrd.img, it would flash an error saying it couldn't find axios.

    I ran npm install axios.

    everything works now. Not sure which one fixed it, but hopefully this saves somebody else a 2 day headache!