Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript fs.readFile不是一个函数_Javascript_Node.js_Angular_Node Modules_Fs - Fatal编程技术网

Javascript fs.readFile不是一个函数

Javascript fs.readFile不是一个函数,javascript,node.js,angular,node-modules,fs,Javascript,Node.js,Angular,Node Modules,Fs,我对Angular和Nodejs是新手。我目前正在使用Angular 5开发一个Web应用程序。我想创建一个节点模块并将其发布到npm,然后在我的应用程序中使用它。此模块应读取xml文件以创建一些对象。它总是给我同样的错误(fs_1.readFile不是函数)。我不知道是否可以在节点模块中读取xml 我正在尝试创建一个npm包,在这个包中会有一个xml文件。在这个包的一个类中,我想读取xml文件 这是我的index.js: "use strict"; Object.defineProperty

我对AngularNodejs是新手。我目前正在使用Angular 5开发一个Web应用程序。我想创建一个节点模块并将其发布到npm,然后在我的应用程序中使用它。此模块应读取xml文件以创建一些对象。它总是给我同样的错误(
fs_1.readFile不是函数
)。我不知道是否可以在节点模块中读取xml

我正在尝试创建一个npm包,在这个包中会有一个xml文件。在这个包的一个类中,我想读取xml文件

这是我的
index.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const xml_js_1= require("xml-js");

class PasswordsConfiguration{
  constructor(){
    this.passwords=new Array();
    this.companies=new Array();
    this._json = undefined;
    this.readXML()

  }
  readXML(){
    var name,isFunction, maskAND, maskOR, key, defaultValue,companyName,displayedName;
    try{

        fs_1.readFile('./passwords-config.xml', 'utf8', (err, data) => {

        // just neglect the rest of the code, it doesn#t enter this function.
        if (err) {

        }
        else {
            this._json = xml_js_1.xml2js(data, {
                compact: true,
                nativeType: true,
                ignoreDeclaration: true,
                ignoreComment: true
            });

            this._json.Configuration.Passwords.every((password) => {
              name =password._attributes.name;
              isFunction=Boolean(password._attributes.isFunction);
              maskAND = parseInt(password._attributes.maskAND,16);
              maskOR = parseInt(password._attributes.maskOR,16);
              key = password._attributes.key;
              defaultValue = parseInt(password._attributes.defaultValue,16);
              this.passwords.push(new Password(name,isFunction, maskAND, maskOR, key, defaultValue));

            });
            this._json.Configuration.Companies.every((company) => {
              companyName =company._attributes.name;
              displayedName =company._attributes.displayedName;
              this.companies.push(new Company(companyName,displayedName));
            });
        }
    });
    }catch(e){

    }

  }
}
exports.PasswordsConfiguration = PasswordsConfiguration;    
这是索引

export declare class PasswordsConfiguration {
  private _json:any;
  passwords:Array<Password>;
  companies:Array<Company>;
  constructor();
  readXML():void;
}
导出声明类密码配置{
private_json:任何;
密码:数组;
公司:阵列;
构造函数();
readXML():void;
}

那么这个功能适用吗?代码中出了什么问题?

您的意思是
fs\u 1。readFile
不是一个函数吗?因为我在你的代码中没有看到
fs.readFile
。可能这是从Typescript编译的JS,fs.readFile在ts文件中。你确定你安装了
fs
模块(查看你的
node\u modules
文件夹)?@Air1
fs
是本机节点模块。它在所有版本的node中都可用,并且不会出现在本地
node\u模块中;i、 e.在浏览器中?这是不可能的。