0
votes

Trying to import xmlbuilder to my typescript class by

import { xmlbuilder } from "xmlbuilder/typings/index";

I got

Error:(2, 10) TS2305: Module '../node_modules/xmlbuilder/typings/index"' has no exported member 'xmlbuilder'.

In the index.d.ts (https://github.com/oozcitak/xmlbuilder-js/blob/master/typings/index.d.ts),

export = xmlbuilder;

declare namespace xmlbuilder { 
  ...
}
2

2 Answers

0
votes

You're trying to import a type definition, not the actual type.

You should use the following code:

import xmlbuilder from "xmlbuilder"

Type definitions are simply ways to describe to TypeScript how the JavaScript object should look like. This makes possible to files written in JavaScript to be interpreted by TypeScript compiler and compiled JS files written in TypeScript to be used from JS files.


As noted in the comments, when modules doesn't have a default export, you should use an alias, importing the entire module.

import * as xmlbuilder from "xmlbuilder"

This is the equivalent of using require("xmlbuilder").

0
votes

Namespaces are not recommended by many, and in this case it would make more sense for you to use a module definition.

This is written like:

declare module 'xmlbuilder' {
  // export types that describe the module here
}

Now when you import xmlbuilder it should automatically have the correct types (provided you have configured your tsconfig correctly).

You will need to define a path to your custom typeRoots (the directory for your type declarations) in your tsconfig.

Some more info about defining typeRoots can be found here.

You can read more about the differences between namespaces and module declarations here.