How to use OSDK in a simple Node Application?

I created an OSDK out of my ontology, and I want to do a simple hello-world

Steps followed:

  • npm init -y
  • npm i typescript
  • npx tsc --init

However, after copy pasting the example documentation in Developer Console, I get:
No "exports" main defined in [...]@osdk/client/package.json.

Is there something I’m doing wrong ? What configuration changes do I need to perform so that OSDK v2 is working for a simple hello world following the docs provided ?

1 Like

I’m running into the exact same issue. Trying to upgrade from 1.1 and seeing the same error.

I am guessing this has to do with you tsconfig file. Try bootstrap the app based on the @osdk/create-app command in Dev console and compare your config.

Hi, I am experiencing the same error. My bootstrapped app works fine, but I need to integrate OSDK with an already existing typescript app. I have commonjs module in tsconfig. How should I change tsconfig to fix the error? An example code would also help. Maybe, import is different?

here is what I use:

{
  "compilerOptions": {
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

and

{
  "compilerOptions": {
    "composite": true,
    "skipLibCheck": true,
    "module": "ESNext",
    "moduleResolution": "bundler",
    "allowSyntheticDefaultImports": true
  },
  "include": ["vite.config.ts"]
}

vite config is basic:

import react from "@vitejs/plugin-react";
import { defineConfig } from "vite";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  base:
    process.env.NODE_ENV === "development"
      ? process.env.DEV_SERVER_BASE_PATH
      : undefined,
  server: {
    port: Number(process.env.DEV_SERVER_PORT ?? 8080),
    host: process.env.DEV_SERVER_HOST,
  },
});
1 Like

Thanks for the example. Do you happen to have example code for OSDK with Express API Framework?

I think you asked this here.

Express is just a library for doing web-server stuff in nodejs so the basic config should be fine assuming you are using the latest ts version and basic config but we will get you an answer shortly.

@VincentF , any luck solving this? I’m also trying to run a simple nodejs app and I’m facing the same error.

So the steps I followed:

  • npm init -y
  • npm i typescript
  • npx tsc --init
    Follow the steps on the Developer console
  • node --version
    with a potential upgrade of node:
  • nvm install node
  • nvm use node
    Create the npmrc file for the repository
  • touch .npmrc
  • nano .npmrc and drop the snippet from developer console
  • Trigger the installation of the SDK npm install @my-osdk-app/sdk@^0.1.0 @osdk/client@^2.0.9 @osdk/oauth@^1.0.0
  • Generate a 2.0 SDK version + install the new version @my-osdk-app/sdk@^0.2.0
  • Add the hello world example code generated in Developer Console in my TS file.
  • Add a few scripts
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js",
    "dev": "node --loader ts-node/esm src/index.ts"
  },

so that one can run npm run build or npm run dev

It seems that by default, a CommonJS package is created, which is not supported with OSDK.
Hence you need to change it for an ES Module.

The main trick for me has been to change the values in tsconfig.json to:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ES2020",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true
  },
  "include": ["src/**/*"]
}

And to add "type": "module" in package.json:

{
  "name": "tmp_osdk_app_test",
...
  "scripts": {
    "build": "tsc",
...
  },
...
  "type": "module"
}

Running npm run dev"

(some warnings)
[
  Object <Object <[Object: null prototype] {}>> {},
  Object <Object <[Object: null prototype] {}>> {},
  Object <Object <[Object: null prototype] {}>> {},
  Object <Object <[Object: null prototype] {}>> {},
  Object <Object <[Object: null prototype] {}>> {},
  Object <Object <[Object: null prototype] {}>> {},
  Object <Object <[Object: null prototype] {}>> {},
  Object <Object <[Object: null prototype] {}>> {},
  Object <Object <[Object: null prototype] {}>> {},
  Object <Object <[Object: null prototype] {}>> {}
]

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.