【问题标题】:Not cannot find module 'fs': I have tried all the link available to resolve the issue没有找不到模块'fs':我已经尝试了所有可用的链接来解决这个问题
【发布时间】:2018-12-01 17:56:55
【问题描述】:

我正在尝试使用 unlink 功能从本地系统中删除文件。但出现找不到模块“fs”的错误。以下是为您提供一些见解的文件。 在我的

app.component.ts

import * as fs from 'fs';
export class Component {
 var filepath = 'C:/Users/[username]/Downloads/test.txt';
 fs.unlink(filepath);

}

但是给出了找不到模块'fs'的错误

 package.json
{
"name": "view-evidence-app",
"version": "0.0.0",
"license": "MIT",
 "scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --dev",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.2.0",
"@angular/common": "^5.2.0",
"@angular/compiler": "^5.2.0",
"@angular/core": "^5.2.0",
"@angular/forms": "^5.2.0",
"@angular/http": "^5.2.0",
"@angular/platform-browser": "^5.2.0",
"@angular/platform-browser-dynamic": "^5.2.0",
"@angular/router": "^5.2.0",
"@types/node": "^6.0.60",
"@types/xlsx": "0.0.36",
"angular-file-saver": "^1.1.3",
"angular-font-awesome": "^2.0.3",
"core-js": "^2.4.1",
"file-saver": "^1.3.8",
"file-saver-typescript": "^1.0.1",
"file-system": "^2.2.2",
"file-url": "^2.0.2",
"font-awesome": "^4.7.0",
"fs": "0.0.1-security",
"path": "^0.12.7",
"primeng": "^5.2.7",
"rxjs": "^5.5.6",
"ts-xlsx": "0.0.11",
"zone.js": "^0.8.19"
 },
"devDependencies": {
"@angular-devkit/build-angular": "~0.6.8",
"@angular/cli": "^6.0.8",
"@angular/compiler-cli": "^5.2.0",
"@angular/language-service": "^5.2.0",
"@types/jasmine": "~2.8.3",
"@types/jasminewd2": "~2.0.2",
"@types/node": "^6.0.60",
"angular-ide": "^0.9.39",
"codelyzer": "^4.0.1",
"jasmine-core": "~2.8.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~2.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~4.1.0",
"tslint": "~5.9.1",
"typescript": "~2.5.3"
 }
 }

 webpack.config.js

 var path = require("path")
 const nodeExternals = require('webpack-node-externals');
 var webpack = require('webpack');

 module.exports = {
"entry": "./lib/index.js"
 ,   "output": {
    "path": __dirname + "/build"
 ,  "filename": "xxhash.js"
 ,  "library": "XXH"
 ,  "libraryTarget": "umd"
 }
 ,   "target": "node"

 ,"node": {
  "fs": "empty",
 }  
 ,    "externals":{
        "fs":    "commonjs fs",
        "path":  "commonjs path"
    }
    }

我在 webpack.config.js 中添加了一行

  var path = require("path")
  const nodeExternals = require('webpack-node-externals');//added
  var webpack = require('webpack');//added

   module.exports = {
  "entry": "./lib/index.js"
  ,   "output": {
    "path": __dirname + "/build"
   ,    "filename": "xxhash.js"
  , "library": "XXH"
    ,   "libraryTarget": "umd"
    }
  ,   "target": "node"     //added

   ,"node": {         //added
  "fs": "empty",
    }   
   ,      "externals":{           //added
        "fs":    "commonjs fs",
        "path":  "commonjs path"
      }
     }

我参考的链接:

Cannot find module 'fs'

cannot-find-module-fs-when-using-webpack

Uncaught Error: Cannot find module “fs” when using webpack

【问题讨论】:

  • 我想你正试图从浏览器(角度)中删除本地系统文件。由于涉及安全风险,这是不可能的。
  • 但是错误应该出现在运行时,我在编译时遇到错误,这是什么原因。@Dhyey
  • 您尝试使用的库 fsfile-system 都用于 node 而不是 Angular,这就是您遇到编译时错误的原因
  • 是的,这两个库都是 node 的,所以如何在我的程序中使用它..我已经安装了 node.js

标签: node.js angular npm


【解决方案1】:

使用文件系统 npm 模块进行文件系统使用 fs 模块似乎已被弃用。

npm install file-system --save

import * as fs from 'file-system';

希望它会起作用!

【讨论】:

  • 我也用过这个,但这并没有任何删除文件的方法,当我使用 filesystem.write() 时,它内部只包含'fs'并且错误来自'fs'不能已解决。@eduPeeth
猜你喜欢
  • 2021-03-16
  • 1970-01-01
  • 2021-09-05
  • 2021-10-30
  • 2023-01-13
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
相关资源
最近更新 更多