0
votes

I found this error :

"Module not found: Can't resolve '../http-common'" when a start the react project

I already try to removing package-lock.json and the node-modules folder, run npm install and then npm start again

But it's doesn't work and it failed to compile

import React from 'react';
import http from "../http-common";

class PersonnageDataService{
    getAll(){
        return http.get("/personnages");
    }

    get(id){
        return http.get(`/personnages/${id}`);
    }

    create(data){
        return http.post("/personnages", data);
    }

    update(id, data){
        return http.put(`/personnages/${id}`, data);
    }

    delete(id){
        return http.delete(`/personnages/${id}`);
    }

    deleteAll(){
        return http.delete(`/personnages`);
    }

}

export default new PersonnageDataService();

and this is my package.json file

{
  "name": "mearnclient",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.5",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "axios": "^0.21.0",
    "bootstrap": "^4.5.3",
    "react": "^17.0.1",
    "react-bootstrap": "^1.4.0",
    "react-dom": "^17.0.1",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.0",
    "web-vitals": "^0.2.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

Do you have any solutions ?

1

1 Answers

0
votes

If http-common is a module defined outside of your project you should reference it as 'http-common' and declare it in dependencies inside package.json.

If http-common is a js file inside your project, it should be available in the parent folder of PersonnageDataService. Example: if PersonnageDataService is in src/services/ then you need a http-common.js inside src.