Not sure if you see an error message or not, or what you mean by “not working”, but I have created an issue to check that (see below).
But in general, it is important to understand that a Page in Hugo can be either a list page or a leaf page (leaf = no children).
So when you do a {{ range first 5 .Data.Pages.ByDate }} you get a list of the pages that is a child of that page. For the home page, this is all the pages, for a section, it is the pages in that section etc. For a regular content page, there aren’t any pages to show (no children).
Depending on what you want to show, this may be an option:
<div id="sidebar">
{{ if .IsPage }}
{{ range first 5 .Site.Pages.ByDate }}
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
{{ end }}
{{ else }}
{{ range first 5 .Data.Pages.ByDate }}
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
{{ end }}
{{ end }}
</div>
```
https://github.com/spf13/hugo/issues/2947