1

I am reading a tutorial on Express and Node.js and trying to convert the JavaScript to TypeScript.

The code includes

var handlebars = require("express-handlebars")
    .create({ defaultLayout: 'main' });

which I have translated to TypeScript as is.

Is there a more idiomatic TypeScript using import ... as (from ECMAScript 6) or import handlebars = ... that accomplishes the same thing (including the call to create)?

I have tried the following:

import handlebars = require("express-handlebars")
handlebars.create({ defaultLayout: 'main' })

but I get a runtime error from nodejs on the later line

app.engine('handlebars', handlebars.engine)

saying Error: callback function required.

I assume that the error comes because the object has not been properly created.

5
  • Try using the 'new' ES6 style syntax: import * as handlebars from 'express-handlebars' Commented Dec 19, 2015 at 13:57
  • I tried that, but when I add handlebars.create(...) as the next line, I get the nodejs error indicated above. Commented Dec 19, 2015 at 14:01
  • 1
    I do not know this package but looking at the exports there is no engine exported. I would guess it is import * as handlebars from 'express-handlebars'; var engine = handlebars.create(...).engine; app.engine('handlebars, engine) Commented Dec 19, 2015 at 15:05
  • That worked. Thanks. Commented Dec 19, 2015 at 19:58
  • You are welcome. Converted the comment as an answer so that you can accept it Commented Dec 20, 2015 at 5:51

1 Answer 1

2

I do not know this package but looking at the exports there is no engine exported.

My guess is

import * as handlebars from 'express-handlebars'; 
const engine = handlebars.create(...).engine; 
app.engine('handlebars', engine);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.