Node.js + Selenium: Cannot find module 'node:events'

780 views
Skip to first unread message

Gage

unread,
Feb 19, 2023, 12:30:06 AM2/19/23
to Selenium Users
This one's got me scratching my head a bit. I just started a new Selenium project for a client and it's my first time writing a Selenium project for a while, but I'm running into a blocker immediately where one of Selenium's module files can't find a core API of Node.

For context on what I've done so far I have updated Node, cleaned out my cache, and re-installed the dependencies a couple of times.

The error is very simple for me to trigger, all I have to do is require in the `selenium-webdriver` module and even if that import is the only line in the code it will still throw the error.
Just for reference, this is the only line in the entire file, the rest was removed to narrow down the bug:
    require("selenium-webdriver");

And this is the error I get when running that file:
   
Error: Cannot find module 'node:events'

Selenium's import of this module is located in `node_modules\selenium-webdriver\bidi\index.js`:  
    const { EventEmitter } = require('node:events')

This is the odd part (for me at least, if there's a reason for this I'd love to hear it to learn why it's doing this) is that if I change it from `node:events` to just `events` then it works fine. The entire script will run and it'll run all of the tests as expected when I paste them back into the file.   
    const { EventEmitter } = require('events')

If I were only running this locally then I'd just change the import and call it a day, but this will be deployed onto a server where I will not be able to alter imports within node_modules. So I need to try to find a solution to get Selenium to recognize the events API from Node without altering the module's files.

Can anyone think of a reason I could be running into this issue? And possible solutions?
Reply all
Reply to author
Forward
0 new messages