简体   繁体   中英

Module not found: Error: Can't resolve 'fs' . Read the details first

I've been building a basic weather api with React and TypeScript. I have utilized "npx create-react-app. typescript" for the setup. After building out my application I started it up. Initially I had success contacting the api however as I tried to save the api calls using fs.writeFile in addition to fs.readFile to collect the data I ran into the error that said it cannot resolve 'fs'.

current ts.config

{
  "compilerOptions": {
    "paths": {
      "crypto": [
        "./node_modules/crypto-browserify"
      ],
    },
    "target": "ES2016",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext",
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "noResolve" : false,
    "types": ["node"],
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "downlevelIteration": true,
  },
  "include": [
    "src"
  ],
  "exclude":["node_modules"]
}

Tried to reset using npm install --save fs.

Deleted node_modules, package.lock.json, removed "fs": "0.0.1-security", and reinstalled. This actually seemed to work but then fs wouldn't be recognized and I reinstalled then got a new error which is progress lol.

Error with getWeather, TypeError: fs__WEBPACK_IMPORTED_MODULE_1___default(...).readFile is not a function at postWeather (App.tsx:51:1) at App.tsx:25:1

I have checked stack overflow for existing material such as Module not found: Error: Can't resolve 'fs' in .

The methods there haven't been successful and I am still searching for alternative ways but if anyone could give some advise I would appreciate it.

The fs module is part of the Node.js modules, React is a frontend framework that runs on the browser, not on the Node.js runtime. Even if installing an external package for fs I doubt it will work, you're gonna need another way to store data on the server, say, an API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM