NodeJS: Typescript export expressjs from wrapper

Viewed 456

I've created an ExpressJS wrapper on private npm repo and i want export my lib and expressjs itself, eg.:

index.ts

export { myExpress } from './my-express'; // my express wrapper
import * as express from 'express';
export = express; // doesn't works!

how can i make it in typescript?

2 Answers

The problem with Express 4 is that it uses deprecated CommonJS export convention that isn't compatible with ES modules, it contains the only export (express() factory), the rest of exports are defined as its properties. It uses legacy TypeScript export = ... syntax, so it cannot be re-exported as ES module export * from 'express', also cannot make use of synthetic default re-export, export { default } from 'express'.

Given the module should re-export an export from express and myExpress export from ./my-express, it should be:

import * as express from 'express';
export default express;

export { myExpress } from './my-express';

It can be imported as:

import express, { myExpress } from '...';

For regular ES module, * could be re-exported:

export * from 'some-package';
export { myExport } from './my-package';

The export = express; syntax isn't correct.

You'll want to do either:

import { myExpress } from './my-express';
import * as express from 'express';

export {
  myExpress,
  express
}

or

import { myExpress } from './my-express';
export { myEpxress }

import * as express from 'express';
export { express }
Related