Skip to content

Example code in docs doesn't work when NodeNext is used for module resolution #273

Open

Description

@Krista-Rogers

Using this tsconfig.json I can get the example code to build without error.

tsconfig.json:

{
  "compilerOptions": {
    "lib": ["ES2022"],
    "outDir": "./dist",
    "rootDir": "./src",
    "target": "ES2022",
    "module": "commonjs",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true
  }
}

Code from website - https://docs.databricks.com/en/dev-tools/nodejs-sql-driver.html#language-TypeScript:

import { DBSQLClient } from '@databricks/sql';
import IDBSQLSession from '@databricks/sql/dist/contracts/IDBSQLSession';
import IOperation from '@databricks/sql/dist/contracts/IOperation';

const serverHostname: string = process.env.DATABRICKS_SERVER_HOSTNAME || '';
const httpPath: string       = process.env.DATABRICKS_HTTP_PATH || '';
const token: string          = process.env.DATABRICKS_TOKEN || '';

if (serverHostname == '' || httpPath == '' || token == '') {
  throw new Error("Cannot find Server Hostname, HTTP Path, or personal access token. " +
                  "Check the environment variables DATABRICKS_SERVER_HOSTNAME, " +
                  "DATABRICKS_HTTP_PATH, and DATABRICKS_TOKEN.");
}

const client: DBSQLClient = new DBSQLClient();
const connectOptions = {
  host: serverHostname,
  path: httpPath,
  token: token
};

client.connect(connectOptions)
  .then(async client => {
    const session: IDBSQLSession = await client.openSession();

    const queryOperation: IOperation = await session.executeStatement(
      'SELECT * FROM samples.nyctaxi.trips LIMIT 2',
      {
        runAsync: true,
        maxRows: 10000 // This option enables the direct results feature.
      }
    );

    const result = await queryOperation.fetchAll();

    await queryOperation.close();

    console.table(result);

    await session.close();
    client.close();
  })
  .catch((error) => {
    console.error(error);
});

However, when changing the tsconfig to use NodeNext (like the below):

{
  "compilerOptions": {
    "lib": ["ES2022"],
    "outDir": "./dist",
    "rootDir": "./src",
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "resolveJsonModule": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true
  }
}

I see the following error on the imports:
Cannot find module '@databricks/sql/dist/contracts/IDBSQLSession' or its corresponding type declarations.ts(2307)
Cannot find module '@databricks/sql/dist/contracts/IOperation' or its corresponding type declarations.ts(2307)

Versions:

  • node 22
  • this is the package.json:
{
  "name": "example",
  "version": "1.0.0",
  "description": "show example of issue",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "build": "tsc"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@databricks/sql": "^1.9.0"
  },
  "devDependencies": {
    "typescript": "^5.6.3",
    "typescript-eslint": "^8.14.0"
  }
}

Activity

  1. Krista-Rogers commented on Jan 24, 2025

    @Krista-Rogers
    Author

    I worked around this by changing the imports to be like so:

    import type IDBSQLSession from "@databricks/sql/dist/contracts/IDBSQLSession.d.ts";
    import type IOperation from "@databricks/sql/dist/contracts/IOperation.d.ts";
    

    Also where the types were used I added .default, for example:

        const session: IDBSQLSession.default = await client.openSession();
    
  2. changed the title [-]Imports don't work when NodeNext is used for module resolution[/-] [+]Example code in docs doesn't work when NodeNext is used for module resolution[/+] on Jan 24, 2025
  3. jackyhu-db commented on Feb 14, 2025

    @jackyhu-db
    Collaborator

    Hi @KristaHyer , the IDBSQLSession is the default export of @databricks/sql/dist/contracts/IDBSQLSession and the generated IDBSQLSession.d.ts is correct and expected, it should work with both node and ES2022. I just verified your tsconfig.json with ES2022 and it can be compiled w/o error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions