Module not found: Error: Can't resolve 'fs' when using readFileSync

馋奶兔 提交于 2021-01-29 20:33:53

问题


I am trying to read a file from typescript and I'm using 'fs' module.

import { readFileSync } from 'fs'
import fs from 'fs'

const fetchSavedCards = (): string[] => {
    fs.readFileSync('saved-card-details.txt','utf8');
    ...

};

Following error is given when building the project.

ERROR in ./service/Service.ts Module not found: Error: Can't resolve 'fs' in '/project_path/front-end/src/service' @ ./service/Service.ts 1:0-34 29:15-27 @ ./component/body/cart/Cart.tsx @ ./component/body/Body.tsx @ ./App.tsx @ ./index.tsx Child html-webpack-plugin for "index.html": 1 asset Entrypoint undefined = index.html [0] ../node_modules/html-webpack-plugin/lib/loader.js!./index.html 412 bytes {0} [built] + 3 hidden modules Child mini-css-extract-plugin ../node_modules/css-loader/index.js!../node_modules/bootstrap/dist/css/bootstrap.css: Entrypoint mini-css-extract-plugin = * 2 modules Child mini-css-extract-plugin ../node_modules/css-loader/index.js!../node_modules/bootstrap/dist/css/bootstrap.min.css: Entrypoint mini-css-extract-plugin = * 2 modules Child mini-css-extract-plugin ../node_modules/css-loader/index.js!css/site.css: Entrypoint mini-css-extract-plugin = * [0] ../node_modules/css-loader!./css/site.css 1.89 KiB {0} [built] + 1 hidden module npm ERR! code ELIFECYCLE npm ERR! errno 2 npm ERR! front-end@1.0.0 build: webpack --mode production && mkdir -p ../proxy/src/main/resources/static && rm -rf ../proxy/src/main/resources/static && rm -rf ../proxy/src/main/resources/templates && mkdir -p ../proxy/src/main/resources/templates && mv dist/index.html ../proxy/src/main/resources/templates/index.html && mv dist/ ../proxy/src/main/resources/static npm ERR! Exit status 2 npm ERR! npm ERR! Failed at the front-end@1.0.0 build script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR! /home/ruchira/.npm/_logs/2020-03-11T03_49_13_767Z-debug.log

My package.json file has 'fs' dependency added.

    "dependencies": {
    "@material-ui/core": "^3.9.4",
    "@material-ui/icons": "^3.0.2",
    "@types/enzyme": "^3.10.5",
    "@types/enzyme-adapter-react-16": "^1.0.6",
    "@types/i18next-browser-languagedetector": "^2.0.2",
    "@types/i18next-xhr-backend": "^1.4.2",
    "@types/react-i18next": "^8.1.0",
    "babel-plugin-transform-decorators-legacy": "^1.3.5",
    "bootstrap": "^4.4.1",
    "enzyme-to-json": "^3.4.4",
    "es6-promise": "^4.2.8",
    "file-system": "^2.2.2",
    "fs": "0.0.1-security",
    "i18next": "^13.1.5",
    "i18next-browser-languagedetector": "^2.2.4",
    "i18next-xhr-backend": "^1.5.1",
    "isomorphic-fetch": "^2.2.1",
    "jquery": "^3.4.1",
    "line-reader": "^0.4.0",
    "mini-css-extract-plugin": "^0.4.1",
    "mobx": "^5.15.4",
    "mobx-react": "^5.4.4",
    "react": "^16.13.0",
    "react-bootstrap": "^0.32.4",
    "react-dom": "^16.13.0",
    "react-i18next": "^8.4.0",
    "react-icons": "^3.9.0",
    "react-popup": "^0.9.3",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1"
  }

NPM version: 6.14.2 Node version: 11.9.0

Can someone help me to resolve this issue

来源:https://stackoverflow.com/questions/60629293/module-not-found-error-cant-resolve-fs-when-using-readfilesync

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!