Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

error TS2304: Cannot find name 'require'. #165

Open
robeverett opened this issue Aug 8, 2018 · 1 comment
Open

error TS2304: Cannot find name 'require'. #165

robeverett opened this issue Aug 8, 2018 · 1 comment

Comments

@robeverett
Copy link

robeverett commented Aug 8, 2018

I'm trying to return a SOAP response in Angular 6.0 httpClient typescript, so I need to convert XML to JSON.

How do I used NodeRequire in typescript ?

I'm trying to do this:
eturn this.http.post(environment.engageServiceUrl + '?op=RetrieveEmployees', soapPayload, httpOptions) .map(data => { var xmlJsn = require('node_modules/xml2js'); console.log(data); },

but it won't build.

I've tried to follow this example:
https://stackoverflow.com/questions/36368405/how-to-parse-xml-in-angular-2

After running npm install.... and adding this in my service.ts:
import * as xml2js from 'node_modules/xml2js';

It builds but I get this warning:

[ts] Could not find a declaration file for module 'node_modules/xml2js'...
Try 'npm install @types/xml2js' if it exists or add a new declaration (d.ts) file containing 'declare module 'xml2js';`

I did that and still get the same.

@xamgore
Copy link

xamgore commented Aug 9, 2019

The package is written in pure javascript, so there are no type definitions of toJson & toXml functions. That's why the compiler raises an error.

You can define a declaration module yourself (depends on tsconfig.json). For example, put the following file into types/xml2json.d.ts:

declare module 'xml2json' {

  export type ToJsonOptions = {
    // Returns a Javascript object instead of a JSON string
    object: boolean;
    // Makes the JSON reversible to XML
    reversible: boolean;
    // Makes type coercion. i.e.: numbers and booleans present in attributes and
    // element values are converted from  string to its correspondent data types.
    // Coerce can be optionally defined as an object with specific methods of
    // coercion based on attribute name or tag name, with fallback to default coercion.
    coerce: boolean;
    // Sanitizes the following characters present in element values: < > ( ) # & " '
    sanitize: boolean;
    // Removes leading and trailing whitespaces as well as line terminators in element values.
    trim: boolean;
    // XML child nodes are always treated as arrays NB: you can specify
    // a selective array of nodes for this to apply to instead of the whole document.
    arrayNotation: boolean;
    // Changes the default textNode property from $t to _t when option is set to true.
    // Alternatively a string can be specified which will override $t to what ever the string is.
    alternateTextNode: boolean;
  }

  export type ToXmlOptions = {
    // Sanitizes the following characters present in element values: < > ( ) # & " '
    sanitize: boolean;
    // Ignores all null values
    ignoreNull: boolean;
  }

  export function toJson(xml: string, options?: Partial<ToJsonOptions>): string | object
  export function toXml(json: string, options?: Partial<ToXmlOptions>): string
}

Usage:

import xml2json from 'xml2json'

const json = xml2json.toJson(str) as string
const data = xml2json.toJson(str, { object: true }) as SomeYoursData

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants