我正在用TypeScript编写React-Native应用程序。一开始,我不能使用'fs',因为我不断得到这样的错误:'Cannot find module 'fs' or its corresponding type declarations',然后我在StackOverflow上查找了这个错误,并将我的package.json更改为:
{
"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"
}
}
然后tsconfig到:
{
"$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"
]
}
这个错误似乎已经存在了(至少VS Code编辑器没有显示给我),但是当我试图运行代码时,我得到了另一个错误:错误:无法从C:\Users-\Desktop\projects\meandyou\Components\complementsFunc.tsx解析模块fs:在项目或以下目录中找不到文件系统:节点模块 1| import * as fs from 'fs'; 我尝试了
npm install,使用其安装程序
npm i @types/node --save-dev`修复Node.js,但没有任何帮助。请帮我解决这个问题。
1条答案
按热度按时间cmssoen21#
你不能使用node的fs,因为它是nodejs环境的一部分。
如果您试图访问设备内的文件,可以使用expo-file-system或react-native-fs