title: Snowflake NodeJs Error: Cannot find module './data/packed/latest.json'
body: <p>The following code runs well in local NodeJS process in windows. However, it fails in AWS lambda.</p>
  
  <p>I don't quite understand why it can't load module: <strong>Cannot find module './data/packed/latest.json'</strong></p>
  
  <p>The lambda event input is:</p>
  
  <pre><code>{
      "Service": "snowflake",
      "Profile": "testetl",
      "Interval": 10,
      "Content": "select * from mytable limit 20",
      "Data": null
  }
  </code></pre>
  
  <p>The credential is provided in Environment Variable:
  testetl: {account:'[my-account]',accessUrl:'<a href="https://[my-account].[my-region].snowflakecomputing.com" rel="nofollow noreferrer">https://[my-account].[my-region].snowflakecomputing.com</a>',username:'[my-username]',password:'[my-password]',warehouse:'[my-data-warehouse]',database:'[my-db]',jsTreatIntegerAsBigInt:true}</p>
  
  <pre class="lang-js prettyprint-override"><code>function connectSnowflake(connection) {
      return new Promise((resolve, reject) =&gt; {
          connection.connect((err, conn) =&gt; {
              if (err)
                  reject(err);
              else
                  resolve(conn);
          });
      });
  }
  function executeSnowflake(connection, sqlText) {
      return new Promise((resolve, reject) =&gt; {
          connection.execute({
              sqlText: sqlText,
              streamResult: true,
              complete: (err, stmt, rows) =&gt; {
                  if (err)
                      reject(err);
                  else
                      resolve(stmt);
              }
          });
      });
  }
  function readSnowflakeRows(statement, start, end) {
      return new Promise((resolve, reject) =&gt; {
          let rows = [];
          statement
              .streamRows({ start: start, end: end })
              .on('error', (err) =&gt; {
              throw err;
          })
              .on('data', (row) =&gt; {
              for (let key in row) {
                  rows.push(row[key]);
                  break;
              }
          })
              .on('end', () =&gt; resolve(rows));
      });
  }
  const snowflake = require('snowflake-sdk');
  exports.handler = async (event) =&gt; {
      const connection = snowflake.createConnection(eval(`(${process.env[event.Profile]})`));
      let connectionInstance = await connectSnowflake(connection);
      let id = connectionInstance.getId();
      let statement = await executeSnowflake(connection, event.Content);
      let rowsCount = statement.getNumRows();
      let maxRows = Math.min(1000, rowsCount);
      event.Values = await readSnowflakeRows(statement, 0, maxRows);
      return event;
  };
  </code></pre>
  
  <p>The error details:</p>
  
  <pre><code>{
      "errorType": "Runtime.ImportModuleError",
      "errorMessage": "Error: Cannot find module './data/packed/latest.json'\nRequire stack:\n- /var/task/node_modules/moment-timezone/index.js\n- /var/task/node_modules/snowflake-sdk/lib/connection/result/sf_timestamp.js\n- /var/task/node_modules/snowflake-sdk/lib/connection/result/column.js\n- /var/task/node_modules/snowflake-sdk/lib/connection/result/result.js\n- /var/task/node_modules/snowflake-sdk/lib/connection/statement.js\n- /var/task/node_modules/snowflake-sdk/lib/connection/connection.js\n- /var/task/node_modules/snowflake-sdk/lib/core.js\n- /var/task/node_modules/snowflake-sdk/lib/snowflake.js\n- /var/task/node_modules/snowflake-sdk/index.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
      "stack": [
          "Runtime.ImportModuleError: Error: Cannot find module './data/packed/latest.json'",
          "Require stack:",
          "- /var/task/node_modules/moment-timezone/index.js",
          "- /var/task/node_modules/snowflake-sdk/lib/connection/result/sf_timestamp.js",
          "- /var/task/node_modules/snowflake-sdk/lib/connection/result/column.js",
          "- /var/task/node_modules/snowflake-sdk/lib/connection/result/result.js",
          "- /var/task/node_modules/snowflake-sdk/lib/connection/statement.js",
          "- /var/task/node_modules/snowflake-sdk/lib/connection/connection.js",
          "- /var/task/node_modules/snowflake-sdk/lib/core.js",
          "- /var/task/node_modules/snowflake-sdk/lib/snowflake.js",
          "- /var/task/node_modules/snowflake-sdk/index.js",
          "- /var/task/index.js",
          "- /var/runtime/UserFunction.js",
          "- /var/runtime/index.js",
          "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
          "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
          "    at Object.&lt;anonymous&gt; (/var/runtime/index.js:43:30)",
          "    at Module._compile (internal/modules/cjs/loader.js:955:30)",
          "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)",
          "    at Module.load (internal/modules/cjs/loader.js:811:32)",
          "    at Function.Module._load (internal/modules/cjs/loader.js:723:14)",
          "    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)",
          "    at internal/main/run_main_module.js:17:11"
      ]
  }
  </code></pre>
  
  <p>The package.json file is:</p>
  
  <pre><code>{
      "name": "snowflakeclient",
      "version": "1.0.0",
      "main": "index.js",
      "scripts": {
          "build": "tsc"
      },
      "dependencies": {
          "snowflake-sdk": "^1.5.0"
      },
  }
  
  </code></pre>