How to create an express Application with import instead of require2019 Community Moderator ElectionExpressJS How to structure an application?Bad Request: SendStream.error while sending file as responsenode.js and express : how to wait for udp responsenode JS client vs servernodejs oauth2 token regeneration help neededAngularJs + Typescript: error TS2307: Cannot find module 'angular'How nginx proxy to express(node.js)?Why res.data is index.html?How do I connect a react frontend and express backend?solving the error 'Unexpected token < in JSON at position 0 at JSON.parse'How to send github OAuth data to client?

Make a transparent 448*448 image

Theorems like the Lovász Local Lemma?

Could the Saturn V actually have launched astronauts around Venus?

When do we add an hyphen (-) to a complex adjective word?

Be in awe of my brilliance!

How to explain that I do not want to visit a country due to personal safety concern?

Have researchers managed to "reverse time"? If so, what does that mean for physics?

Schematic conventions for different supply rails

Provisioning profile doesn't include the application-identifier and keychain-access-groups entitlements

What are the possible solutions of the given equation?

How could a female member of a species produce eggs unto death?

Where is the 1/8 CR apprentice in Volo's Guide to Monsters?

Possible Leak In Concrete

I need to drive a 7/16" nut but am unsure how to use the socket I bought for my screwdriver

Instead of Universal Basic Income, why not Universal Basic NEEDS?

Does this AnyDice function accurately calculate the number of ogres you make unconcious with three 4th-level castings of Sleep?

Bash: What does "masking return values" mean?

Can anyone tell me why this program fails?

Why must traveling waves have the same amplitude to form a standing wave?

2D counterpart of std::array in C++17

Humanity loses the vast majority of its technology, information, and population in the year 2122. How long does it take to rebuild itself?

How to make healing in an exploration game interesting

Pinhole Camera with Instant Film

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?



How to create an express Application with import instead of require



2019 Community Moderator ElectionExpressJS How to structure an application?Bad Request: SendStream.error while sending file as responsenode.js and express : how to wait for udp responsenode JS client vs servernodejs oauth2 token regeneration help neededAngularJs + Typescript: error TS2307: Cannot find module 'angular'How nginx proxy to express(node.js)?Why res.data is index.html?How do I connect a react frontend and express backend?solving the error 'Unexpected token < in JSON at position 0 at JSON.parse'How to send github OAuth data to client?










2















This is what works with require. We instead want it to use import.



import Request, Response, Application from 'express';

// TODO Figure out how NOT to use require here.
const express = require('express');
var app: Application = express();

app.get('/', function (req: Request, res: Response)
res.send('Hello World')
);

app.listen(3000);


What we have tried



Our tsconfig.json has "esModuleInterop": true.



Attempt # 1



import express from 'express';


That gives this error:




"node_modules/@types/express/index"' has no default export.ts




Attempt # 2



import * as express from 'express';
var app = express();


That gives a different error:




Cannot invoke an expression whose type lacks a call signature. Type 'typeof e' has no compatible call signatures.ts(2349)
index.ts(1, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.











share|improve this question






















  • I can tell you why Attempt #2 didn't work: You asked for a module namespace object, then tried to run it as a function.

    – T.J. Crowder
    Mar 6 at 18:30











  • Looks like @types/express is missing the fact that Express provides a default export that's a function. (I just double-checked that it does, and that import express from "express"; correctly retrieves it in JavaScript code with --experimental-modules enabled.) So definitely a types thing (as the error suggests). Looking in node_modules/@types/express/index.d.ts, I see that it at least tries to define an export for the express function: "Creates an Express application. The express() function is a top-level function exported by the express module."

    – T.J. Crowder
    Mar 6 at 18:35







  • 1





    @T.J.Crowder Doing a forced cast to a Function works: const app = (express as unknown as Function)();. It's certainly an ugly hack, though.

    – Shaun Luttin
    Mar 6 at 19:02







  • 1





    (Speaking of hacks: import * as mno from 'express'; const express = mno.default as unknown as Function;) But the real solution (which sadly I can't help with) is to sort out the types in @types/express/index.d.ts and/or TypeScript's use of them.

    – T.J. Crowder
    Mar 6 at 19:14







  • 1





    Sorting out the types would be a worthwhile pull request. I can add that to my "maybe someday" list of things to do.

    – Shaun Luttin
    Mar 6 at 21:00















2















This is what works with require. We instead want it to use import.



import Request, Response, Application from 'express';

// TODO Figure out how NOT to use require here.
const express = require('express');
var app: Application = express();

app.get('/', function (req: Request, res: Response)
res.send('Hello World')
);

app.listen(3000);


What we have tried



Our tsconfig.json has "esModuleInterop": true.



Attempt # 1



import express from 'express';


That gives this error:




"node_modules/@types/express/index"' has no default export.ts




Attempt # 2



import * as express from 'express';
var app = express();


That gives a different error:




Cannot invoke an expression whose type lacks a call signature. Type 'typeof e' has no compatible call signatures.ts(2349)
index.ts(1, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.











share|improve this question






















  • I can tell you why Attempt #2 didn't work: You asked for a module namespace object, then tried to run it as a function.

    – T.J. Crowder
    Mar 6 at 18:30











  • Looks like @types/express is missing the fact that Express provides a default export that's a function. (I just double-checked that it does, and that import express from "express"; correctly retrieves it in JavaScript code with --experimental-modules enabled.) So definitely a types thing (as the error suggests). Looking in node_modules/@types/express/index.d.ts, I see that it at least tries to define an export for the express function: "Creates an Express application. The express() function is a top-level function exported by the express module."

    – T.J. Crowder
    Mar 6 at 18:35







  • 1





    @T.J.Crowder Doing a forced cast to a Function works: const app = (express as unknown as Function)();. It's certainly an ugly hack, though.

    – Shaun Luttin
    Mar 6 at 19:02







  • 1





    (Speaking of hacks: import * as mno from 'express'; const express = mno.default as unknown as Function;) But the real solution (which sadly I can't help with) is to sort out the types in @types/express/index.d.ts and/or TypeScript's use of them.

    – T.J. Crowder
    Mar 6 at 19:14







  • 1





    Sorting out the types would be a worthwhile pull request. I can add that to my "maybe someday" list of things to do.

    – Shaun Luttin
    Mar 6 at 21:00













2












2








2








This is what works with require. We instead want it to use import.



import Request, Response, Application from 'express';

// TODO Figure out how NOT to use require here.
const express = require('express');
var app: Application = express();

app.get('/', function (req: Request, res: Response)
res.send('Hello World')
);

app.listen(3000);


What we have tried



Our tsconfig.json has "esModuleInterop": true.



Attempt # 1



import express from 'express';


That gives this error:




"node_modules/@types/express/index"' has no default export.ts




Attempt # 2



import * as express from 'express';
var app = express();


That gives a different error:




Cannot invoke an expression whose type lacks a call signature. Type 'typeof e' has no compatible call signatures.ts(2349)
index.ts(1, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.











share|improve this question














This is what works with require. We instead want it to use import.



import Request, Response, Application from 'express';

// TODO Figure out how NOT to use require here.
const express = require('express');
var app: Application = express();

app.get('/', function (req: Request, res: Response)
res.send('Hello World')
);

app.listen(3000);


What we have tried



Our tsconfig.json has "esModuleInterop": true.



Attempt # 1



import express from 'express';


That gives this error:




"node_modules/@types/express/index"' has no default export.ts




Attempt # 2



import * as express from 'express';
var app = express();


That gives a different error:




Cannot invoke an expression whose type lacks a call signature. Type 'typeof e' has no compatible call signatures.ts(2349)
index.ts(1, 1): Type originates at this import. A namespace-style import cannot be called or constructed, and will cause a failure at runtime. Consider using a default import or import require here instead.








typescript express






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 6 at 18:21









Shaun LuttinShaun Luttin

61.5k34233292




61.5k34233292












  • I can tell you why Attempt #2 didn't work: You asked for a module namespace object, then tried to run it as a function.

    – T.J. Crowder
    Mar 6 at 18:30











  • Looks like @types/express is missing the fact that Express provides a default export that's a function. (I just double-checked that it does, and that import express from "express"; correctly retrieves it in JavaScript code with --experimental-modules enabled.) So definitely a types thing (as the error suggests). Looking in node_modules/@types/express/index.d.ts, I see that it at least tries to define an export for the express function: "Creates an Express application. The express() function is a top-level function exported by the express module."

    – T.J. Crowder
    Mar 6 at 18:35







  • 1





    @T.J.Crowder Doing a forced cast to a Function works: const app = (express as unknown as Function)();. It's certainly an ugly hack, though.

    – Shaun Luttin
    Mar 6 at 19:02







  • 1





    (Speaking of hacks: import * as mno from 'express'; const express = mno.default as unknown as Function;) But the real solution (which sadly I can't help with) is to sort out the types in @types/express/index.d.ts and/or TypeScript's use of them.

    – T.J. Crowder
    Mar 6 at 19:14







  • 1





    Sorting out the types would be a worthwhile pull request. I can add that to my "maybe someday" list of things to do.

    – Shaun Luttin
    Mar 6 at 21:00

















  • I can tell you why Attempt #2 didn't work: You asked for a module namespace object, then tried to run it as a function.

    – T.J. Crowder
    Mar 6 at 18:30











  • Looks like @types/express is missing the fact that Express provides a default export that's a function. (I just double-checked that it does, and that import express from "express"; correctly retrieves it in JavaScript code with --experimental-modules enabled.) So definitely a types thing (as the error suggests). Looking in node_modules/@types/express/index.d.ts, I see that it at least tries to define an export for the express function: "Creates an Express application. The express() function is a top-level function exported by the express module."

    – T.J. Crowder
    Mar 6 at 18:35







  • 1





    @T.J.Crowder Doing a forced cast to a Function works: const app = (express as unknown as Function)();. It's certainly an ugly hack, though.

    – Shaun Luttin
    Mar 6 at 19:02







  • 1





    (Speaking of hacks: import * as mno from 'express'; const express = mno.default as unknown as Function;) But the real solution (which sadly I can't help with) is to sort out the types in @types/express/index.d.ts and/or TypeScript's use of them.

    – T.J. Crowder
    Mar 6 at 19:14







  • 1





    Sorting out the types would be a worthwhile pull request. I can add that to my "maybe someday" list of things to do.

    – Shaun Luttin
    Mar 6 at 21:00
















I can tell you why Attempt #2 didn't work: You asked for a module namespace object, then tried to run it as a function.

– T.J. Crowder
Mar 6 at 18:30





I can tell you why Attempt #2 didn't work: You asked for a module namespace object, then tried to run it as a function.

– T.J. Crowder
Mar 6 at 18:30













Looks like @types/express is missing the fact that Express provides a default export that's a function. (I just double-checked that it does, and that import express from "express"; correctly retrieves it in JavaScript code with --experimental-modules enabled.) So definitely a types thing (as the error suggests). Looking in node_modules/@types/express/index.d.ts, I see that it at least tries to define an export for the express function: "Creates an Express application. The express() function is a top-level function exported by the express module."

– T.J. Crowder
Mar 6 at 18:35






Looks like @types/express is missing the fact that Express provides a default export that's a function. (I just double-checked that it does, and that import express from "express"; correctly retrieves it in JavaScript code with --experimental-modules enabled.) So definitely a types thing (as the error suggests). Looking in node_modules/@types/express/index.d.ts, I see that it at least tries to define an export for the express function: "Creates an Express application. The express() function is a top-level function exported by the express module."

– T.J. Crowder
Mar 6 at 18:35





1




1





@T.J.Crowder Doing a forced cast to a Function works: const app = (express as unknown as Function)();. It's certainly an ugly hack, though.

– Shaun Luttin
Mar 6 at 19:02






@T.J.Crowder Doing a forced cast to a Function works: const app = (express as unknown as Function)();. It's certainly an ugly hack, though.

– Shaun Luttin
Mar 6 at 19:02





1




1





(Speaking of hacks: import * as mno from 'express'; const express = mno.default as unknown as Function;) But the real solution (which sadly I can't help with) is to sort out the types in @types/express/index.d.ts and/or TypeScript's use of them.

– T.J. Crowder
Mar 6 at 19:14






(Speaking of hacks: import * as mno from 'express'; const express = mno.default as unknown as Function;) But the real solution (which sadly I can't help with) is to sort out the types in @types/express/index.d.ts and/or TypeScript's use of them.

– T.J. Crowder
Mar 6 at 19:14





1




1





Sorting out the types would be a worthwhile pull request. I can add that to my "maybe someday" list of things to do.

– Shaun Luttin
Mar 6 at 21:00





Sorting out the types would be a worthwhile pull request. I can add that to my "maybe someday" list of things to do.

– Shaun Luttin
Mar 6 at 21:00












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55029813%2fhow-to-create-an-express-application-with-import-instead-of-require%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55029813%2fhow-to-create-an-express-application-with-import-instead-of-require%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко