fs could not be found within the project or in these directories: node_modules.

huangapple go评论72阅读模式
英文:

fs could not be found within the project or in these directories: node_modules

问题

I'm writing React-Native app in TypeScript. At first, I couldn't use 'fs', because I kept getting errors like this: 'Cannot find module 'fs' or its corresponding type declarations', then I looked up this error on StackOverflow and changed my package.json to this:

{
  "name": "meandyou",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "@react-navigation/native-stack": "^6.9.12",
    "babel-plugin-module-resolver": "^5.0.0",
    "babel-plugin-root-import": "^6.6.0",
    "react": "18.2.0",
    "react-native": "0.71.8",
    "react-native-image-slider-banner": "^1.0.3",
    "react-native-safe-area-context": "^4.5.3",
    "react-native-screens": "^3.20.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native-community/eslint-config": "^3.2.0",
    "@react-navigation/native": "^6.1.6",
    "@react-navigation/stack": "^6.3.16",
    "@tsconfig/react-native": "^2.0.2",
    "@types/jest": "^29.2.1",
    "@types/node": "^20.3.0",
    "@types/react": "^18.0.24",
    "@types/react-navigation": "^3.4.0",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.2.1",
    "eslint": "^8.19.0",
    "jest": "^29.2.1",
    "metro-config": "^0.61.0",
    "metro-react-native-babel-preset": "0.73.9",
    "prettier": "^2.4.1",
    "react-native-unimodules": "^0.10.1",
    "react-test-renderer": "18.2.0",
    "typescript": "4.8.4"
  },
  "jest": {
    "preset": "react-native"
  }
}

and tsconfig to this:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "React Native",
  "_version": "2.0.3",
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "types": ["react-native", "jest", "node"],
    "lib": [
      "es2019"
    ],
    "allowJs": true,
    "jsx": "react-native",
    "noEmit": true,
    "isolatedModules": true,
    "strict": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ]
}

The error seemed to seize to exist (at least VS Code editor didn't show it to me), but when I tried to run the code I got another error:
` Error: Unable to resolve module fs from C:\Users-\Desktop\projects\meandyou\Components\complimentsFunc.tsx: fs could not be found within the project or in these directories:
node_modules
> 1 | import * as fs from 'fs';

I tried npm install, fixing Node.js using its installer, npm i @types/node --save-dev, but nothing helps. Please help me fix this problem.

英文:

I'm writing React-Native app in TypeScript. At first, I couldn't use 'fs', because I kept getting errors like this: 'Cannot find module 'fs' or its corresponding type declarations', then I looked up this error on StackOverflow and changed my package.json to this:

{
  "name": "meandyou",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "@react-navigation/native-stack": "^6.9.12",
    "babel-plugin-module-resolver": "^5.0.0",
    "babel-plugin-root-import": "^6.6.0",
    "react": "18.2.0",
    "react-native": "0.71.8",
    "react-native-image-slider-banner": "^1.0.3",
    "react-native-safe-area-context": "^4.5.3",
    "react-native-screens": "^3.20.0"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native-community/eslint-config": "^3.2.0",
    "@react-navigation/native": "^6.1.6",
    "@react-navigation/stack": "^6.3.16",
    "@tsconfig/react-native": "^2.0.2",
    "@types/jest": "^29.2.1",
    "@types/node": "^20.3.0",
    "@types/react": "^18.0.24",
    "@types/react-navigation": "^3.4.0",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.2.1",
    "eslint": "^8.19.0",
    "jest": "^29.2.1",
    "metro-config": "^0.61.0",
    "metro-react-native-babel-preset": "0.73.9",
    "prettier": "^2.4.1",
    "react-native-unimodules": "^0.10.1",
    "react-test-renderer": "18.2.0",
    "typescript": "4.8.4"
  },
  "jest": {
    "preset": "react-native"
  }
}

and tsconfig to this:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "display": "React Native",
  "_version": "2.0.3",
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "types": ["react-native", "jest", "node"],
    "lib": [
      "es2019"
    ],
    "allowJs": true,
    "jsx": "react-native",
    "noEmit": true,
    "isolatedModules": true,
    "strict": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "exclude": [
    "node_modules",
    "babel.config.js",
    "metro.config.js",
    "jest.config.js"
  ]
}

The error seemed to seize to exist (at least VS Code editor didn't show it to me), but when I tried to run the code I got another error:
` Error: Unable to resolve module fs from C:\Users-\Desktop\projects\meandyou\Components\complimentsFunc.tsx: fs could not be found within the project or in these directories:
node_modules
> 1 | import * as fs from 'fs';

I tried npm install, fixing Node.js using its installer, npm i @types/node --save-dev, but nothing helps. Please help me fix this problem.

fs could not be found within the project or in these directories: node_modules.

答案1

得分: 1

你不能使用node的fs,因为它是nodejs环境的一部分。

如果你想访问设备内的文件,可以使用expo-file-systemreact-native-fs

英文:

You're not able to use node's fs because it is part of nodejs environment.

If you are trying to access files inside the device you can use expo-file-system or react-native-fs

huangapple
  • 本文由 发表于 2023年6月12日 04:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定