Search code examples
javascriptsnowflake-cloud-data-platformcypressasync-hookscypress-task

snowflake-sdk: Module not found: Error: Can't resolve 'async_hooks' in 'C:\projectname\node_modules\vm2\lib'


I am trying to integrate Snowflake in my Cypress tests but it keeps on throwing error while compiling. Error:

Error: Webpack Compilation Error
./node_modules/vm2/lib/resolver-compat.js
Module not found: Error: Can't resolve 'async_hooks' in 'C:\snowflake\node_modules\vm2\lib'
resolve 'async_hooks' in 'C:\snowflake\node_modules\vm2\lib'
  Parsed request is a module
  using description file: C:\snowflake\node_modules\vm2\package.json (relative path: ./lib)
    Field 'browser' doesn't contain a valid alias configuration

Tried the following simple steps:

  1. Installed Cypress
  2. Installed snowflake-sdk through npm
  3. Created a spec file and imported:
const snowflake = require("snowflake-sdk");

When I run the spec file, I am getting the error as above.

If I trace the error to the file resolver-compat.js, I can see the import where it fails.

    const {AsyncResource} = require('async_hooks');

I have manually done npm install async_hooks but no luck there also.

package.json

"devDependencies": {
    "cypress": "^11.2.0"
  },
  "dependencies": {
    "async_hooks": "^1.0.0",
    "snowflake-sdk": "^1.6.16"
  }

Solution

  • The snowflake-sdk is a NodeJs package, so in Cypress you need to interact with it via a task.

    Here's a basic connection task taken from the Snowflake docs.

    cypress.config.js

    const { defineConfig } = require("cypress");
    var snowflake = require("snowflake-sdk");
    
    let connectionId;
    
    module.exports = defineConfig({
      e2e: {
        setupNodeEvents(on, config) {
          on("task", {
            snowflake: () => {
              var connection = snowflake.createConnection({
                account: "account",
                username: "user",
                password: "password",
                application: "application",
              });
    
              connection.connect(function (err, conn) {
                if (err) {
                  console.error("Unable to connect: " + err.message);
                } else {
                  console.log("Successfully connected to Snowflake.");
                  // Optional: store the connection ID.
                  connectionId = conn.getId();
                }
              })
              return null
            },
          });
        },
      },
    })
    

    Test

    it('Connect to snowflake', () => {
      cy.task('snowflake')
    })
    

    Without proper credentials, it produces a connection error but proves the package is working.

    Unable to connect: Request to Snowflake failed.