Article summary
My current project is using Cypress for end-to-end browser testing. We recently brought in Prisma when we added a PostgreSQL database to the system.
The Cypress tests need to be able to populate the database with test data at the beginning of a test, and then occasionally retrieve some data to make assertions as tests are execting. We were already using Cypress tasks to execute code outside of the browser during tests, and I came across a number of people describing how they use cy.task
to interact with a database in tasks.
The Error
I had no trouble getting this working in the Docker container environment I described in a previous post. But when other developers on the team tried running Cypress directly on their MacBook Pros, it would fail and they’d see an “Unable to load Node-API Library” error message when loading the plugins.
I found very little when searching for the error, but the fix was easy once I figured it out. I’m sharing it here so, hopefully, others won’t need to spend the same time investigating if they run into the same issue.
When we’d try to run Cypress on a Mac (Big Sur) we were seeing this message:
The following error was thrown by a plugin. We stopped running your
tests because a plugin crashed. Please check your plugins file
Error: Unable to load Node-API Library from
my-proj/node_modules/.prisma/client/libquery_engine-darwin.dylib.node,
Library may be corrupt
The Solution
Cypress comes bundled with a version of Node. For reasons I haven’t bothered to understand, that bundled Node is unable to load the Prisma library on macOS. Apparently this can be a problem with other libraries as well, as described in the documentation for the nodeVersion
configuration:
You may want to use a different Node version if the code executing from the plugins file requires features present in a different Node version from the Node version bundled with Cypress. You can use the Node version detected on your system by setting the nodeVersion configuration to system. For example, you need to use the system Node if you want to load node-sass or sqlite3 modules from your plugins file.
Adding a nodeVersion
property with a value of system
to the cypress.json
file is all it took to fix the problem.
{
"baseUrl": "http://localhost:3010",
"nodeVersion": "system"
}
Fixing the “Unable to load Node-API Library” Error
This was one of those fairly rare instances where something goes wrong and a web search doesn’t help at all. In the end, the fix was easy, but it took me a while to figure it out. Hopefully, this post has saved you some time.