【发布时间】: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"
}
}
我参考的链接:
【问题讨论】:
-
我想你正试图从浏览器(角度)中删除本地系统文件。由于涉及安全风险,这是不可能的。
-
但是错误应该出现在运行时,我在编译时遇到错误,这是什么原因。@Dhyey
-
您尝试使用的库
fs和file-system都用于node而不是 Angular,这就是您遇到编译时错误的原因 -
是的,这两个库都是 node 的,所以如何在我的程序中使用它..我已经安装了 node.js