Search code examples
typescriptdeno

How to export deps in DENO?


I would like to know how I can export npm modules as express For example, I have the following import, it works fine and the linter does not show any errors:

import express, { Request, Response } from 'npm:express';

however when taking it to my deps.ts I get the following error:

export express, { Request, Response } from 'npm:express';

Error:

';' expected.

try to separate it but it gives another error:

export express from 'npm:express'; export { Request, Response } from 'npm:express';

Error:

Unexpected keyword or identifier

Then try the following:

import express from 'npm:express'; export { Request, Response } from 'npm:express'; export { express };

but i get the following error:

Uncaught SyntaxError: The requested module 'npm:express' does not provide an export named 'Request' export { Request, Response } from 'npm:express';

and I don't know how to solve it, I hope you can help me, I don't want to use a default import in my app.ts file, I hope you can help me


Solution

  • The default export from the express package is a function, but Request and Response are type interfaces.

    When exporting types, you should use the type modifier, like this:

    ./deps.ts:

    export { default as express, type Request, type Response } from "npm:express";
    
    

    Then, those exported dependencies can be imported into another module like this:

    ./mod.ts:

    import { express, type Request, type Response } from "./deps.ts";
    
    console.log("typeof express:", typeof express); // typeof express: function
    
    

    You can then run the mod.ts module in the terminal and see this output:

    % deno --version
    deno 1.29.4 (release, x86_64-apple-darwin)
    v8 10.9.194.5
    typescript 4.9.4
    
    % deno run --allow-env --allow-read mod.ts
    typeof express: function