How to fix: $ is not defined on NightmareJS

I keep learning new things. This time advanced web scraping with NightmareJS but soon I faced this issue. Let’s see how to fix $ is not defined on NightmareJS.

$ is not defined on NightmareJS

The cause is the jQuery file is not included in your script. You need to add this:

.inject('js', 'jquery.min.js')

Please note you must have the jQuery file at the same level as the script and the filename should match.

Try and it should work!

Full example

Very easy, just chain the previous function with your jQuery file to your NightmareJS script.

var nightmareScript = new Nightmare()
  .inject('js', 'jquery.min.js') // Look here! 

Then you can evaluate the page and the DOM with jQuery. Happy scraping!

Feel free to leave your doubt or comment 🙂

David Burgos

Read more posts by this author.