Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
KristaHyer opened this issue Jan 24, 2025 · 2 comments

Comments

@KristaHyer
Copy link

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"
  }
}
@KristaHyer
Copy link
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();

@KristaHyer KristaHyer 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 Jan 24, 2025
@jackyhu-db
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants