Instantsearch.js not working when using Express

Super new to web development but I’ve stuck for 2 days so any help is greatly appreciated.
I’ve been trying to use Express framework + Algolia to add a search function to my website. However, I keep getting

instantsearch.js@4.8.3:2 Uncaught Error: Container must be `string` or `HTMLElement`. Unable to find #searchbox
    at ce (instantsearch.js@4.8.3:2)
    at Object.searchBox (instantsearch.js@4.8.3:2)
    at user.js:9

I imported instantsearch.js in my pug file:

script(src="https://cdn.jsdelivr.net/algoliasearch/3.32.0/algoliasearchLite.min.js")
script(src="https://cdn.jsdelivr.net/npm/instantsearch.js@4.8.3")
script(src='/javascripts/user.js', type='module')

and my user.js file

const searchClient = algoliasearch('...', '...');

const search = instantsearch({
  indexName: 'recipes',
  searchClient,
});

search.addWidgets([
  instantsearch.widgets.searchBox({
    container: '#searchbox',
  }),

  instantsearch.widgets.hits({
    container: '#hits',
  })
]);

search.start();

My folder is structured as:

  1. views
    1.1 layout.pug
    1.2 user.pug
  2. index.js
  3. javascripts
    3.1 user.js

Any direction is greatly appreciated. Thanks

Hi @xdai1091

const searchClient = algoliasearch('...', '...');

const search = instantsearch({
  indexName: 'recipes',
  searchClient,
});

search.addWidgets([
  instantsearch.widgets.searchBox({
    container: '#searchbox',
  }),

  instantsearch.widgets.hits({
    container: '#hits',
  })
]);

search.start();

For that code to work properly, you need to have some elements in your HTML page.

In your case, views/user.pug needs to have some elements like

<div id="searchbox"></div>

...

<div id="hits></div>

That two elements will be filled by searchBox and hits widgets.

And here is a code sandbox:

I think it’d be also good if you take some time in that code sandbox to see how things work, and then go back to your express + instantsearch project. It should be a good stepping stone.

Let me know how it goes and if you have any question!