question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`extname` documentation is misleading

See original GitHub issue
OS:                 Windows 7 Pro 64 bit
Node version :      v6.0.0
express-handlebars: ^3.0.0

I find it very unintuitive to have to define extname in my express-handlebars instance to correctly resolve my layouts and partials filepaths.

app.engine('handlebars', hbs({
    extname:        '.hbs'
,   defaultLayout:  'views'
}));

app.set('view engine',  'handlebars');
-- /views
 |-- /layouts
 | `-- main.hbs       # this works
 |-- /partials
 | `-- ...
 |-- home.hbs         # this doesn't
 |-- home.handlebars  # this works
 `-- ...

Originally I was trying to use Using .hbs on my /views/FILENAME.hbs files resulting in a Error: Failed to lookup view "home" in views directory "/MY_PROJECT_PATH/views", but switching it back to .handlebars “fixed” the issue.

I would expect a view engine to resolve the file extension for all view files involved based on the standard Express engine behavior where you pass in the extension to be used on all view files.


After a fair amount of digging into this issue, I determined that I simply overlooked the documentation regarding how extname is utilized and was able to fix my problem per the examples on the docs:

app.engine('.hbs', hbs({
    extname:        '.hbs'
,   defaultLayout:  process.env.HBS_DEFAULT_LAYOUT
}));

app.set('view engine',  '.hbs');

I’m sure newcomers to Express have likely hit this disconnect before, but why is it that extname can’t be derived from the engine instance extension value?


A related issue I found in the source for the engine is in the ExpressHandlebars.prototype._getTemplateName method: https://github.com/ericf/express-handlebars/blob/master/lib/express-handlebars.js#L322-L331

This section of the engine has a potential pitfall I think deserves discussing, and maybe some added testing around it.

The filePath argument contains the file extension we registered our view engine with, in my case .handlebars. However, the regex being used to strip the file extension is using the extname override, which I’ve determined is not influenced by the engine’s registered value. So your regex fails to strip the extension and returns filename.handlebars because it doesn’t match .hbs per my override.

I may be missing an implementation detail here, but I would assume if the goal to was return just the filename, having this kind of a mismatch left up to the developer implementing this with no intelligent debug messaging seems like a real problem to me.

Thoughts?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:17
  • Comments:5

github_iconTop GitHub Comments

2reactions
bmcminncommented, Apr 9, 2019

UPDATE: since this is gaining some traction, I’d like to revisit the issue. Per the snippet I linked to back when, I think the best way to resolve this issue is to update the ExpressHandlebars.prototype._getTemplateName method to leverage the native path library and its basename() method and pulling the extname from the app view engine instance config.

I may take a stab at this later and issue a PR, along with updated Docs.

0reactions
UziTechcommented, Apr 22, 2022

express-handlebars has moved to a new repo https://github.com/express-handlebars/express-handlebars

If this is still an issue please create an issue or PR in that repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Path | Node.js v19.3.0 Documentation
The path.extname() method returns the extension of the path , from the last ... If the given path is a zero-length string, false...
Read more >
How to Get the Extension of a File in Node.js
A tutorial on how to easily get the extension of a file in Node.js. ... path.extname(false);// ❌ TypeError: Received URL instance instead of...
Read more >
How can I get file extensions with JavaScript?
but in this case the file name looks like filname.tes.test.jpg. Kindly consider the output. I hope it will be false. – Fero. Jul...
Read more >
file-extension | Yarn - Package Manager
For 1.x docs, see classic.yarnpkg.com. ... Differences to path.extname : Treats dotfiles as extension ( .eslintrc => eslintrc ); Treats extensionless files ...
Read more >
Is ProjExstate appropriate for this?
Two notes that the documentation leaves out: extname and key are both case insensitive, ... I think the decription is a bit misleading,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found