找不到模块'fs':我尝试了所有可用的链接来解决问题

时间:2018-06-22 09:25:34

标签: node.js angular npm

我正在尝试使用取消链接功能从本地系统删除文件。但是遇到无法找到模块'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

1 个答案:

答案 0 :(得分:0)

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

npm install file-system --save

import * as fs from 'file-system';

希望它能工作!