alpacahq / typescript-sdk

A TypeScript SDK for the https://alpaca.markets REST API and WebSocket streams.
https://docs.alpaca.markets
28 stars 5 forks source link

Cannot find module '@alpacahq/typescript-sdk' or its corresponding type declarations. (tsserver 2307) #4

Open zacharytyhacz opened 3 months ago

zacharytyhacz commented 3 months ago

I have no idea why is happening

All I did was run npm install @alpacahq/typescript-sdk and it successfully installed and my project has no vulnerabilities.

I tried all sorts of different tests and typescript just is not finding this package.

import { createClient } from "@alpacahq/typescript-sdk"             ■  Cannot find module '@alpacahq/typescript-sdk' or its corresponding type declarations. (tsserver 2307)
import config from '../config'

const { ALPACA_API_KEY_ID, ALPACA_API_SECRET_KEY } = config()

const client = createClient({
  key: ALPACA_API_KEY_ID,
  secret: ALPACA_API_SECRET_KEY
})

tsconfig.json

{
    "compilerOptions": {
        "module": "NodeNext",
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "target": "esnext",
        "noImplicitAny": true,
        "noEmit": true,
        "moduleResolution": "NodeNext",
        "sourceMap": false,
        "baseUrl": ".",
        "forceConsistentCasingInFileNames": true,
        "types": [
            "node"
        ]
    }
}

package.json

{
  "name": "alpaca-trader",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "nodemon ./src/index.ts"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@alpacahq/typescript-sdk": "^0.0.29-preview",
    "@types/dotenv-safe": "^8.1.6",
    "@types/node": "^20.14.5",
    "cron": "^3.1.7",
    "dotenv-safe": "^9.1.0",
    "nodemon": "^3.1.3",
    "puppeteer": "^22.11.2",
    "typescript": "^5.4.5"
  }
}

package-lock.json

    "node_modules/@alpacahq/typescript-sdk": {
      "version": "0.0.29-preview",
      "resolved": "https://registry.npmjs.org/@alpacahq/typescript-sdk/-/typescript-sdk-0.0.29-preview.tgz",
      "integrity": "sha512-wkiv+iYV5IaMZni4GTSlWJlLCzh8NdMmmFT6vjeyfPFYNambmfIKRL5QW9mRq/tsUJvLDke2wn4OEGjx9N4Hgw==",
      "dependencies": {
        "@deno/shim-deno": "~0.18.0"
      }
    },

When I play around with my tsconfig.json, sometimes this shows: image

image


this makes the error in my nvim go away

import { createClient } from "@alpacahq/typescript-sdk/esm/mod" // added /esm/mod
...

But when I run with nodemon I get this error

/usr/local/lib/node_modules/ts-node/src/index.ts:820
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/alpaca/index.ts:1:30 - error TS2307: Cannot find module '@alpacahq/typescript-sdk/esm/mod' or its corresponding type declarations.

1 import { createClient } from "@alpacahq/typescript-sdk/esm/mod"
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    at createTSError (/usr/local/lib/node_modules/ts-node/src/index.ts:820:12)
    at reportTSError (/usr/local/lib/node_modules/ts-node/src/index.ts:824:19)
    at getOutput (/usr/local/lib/node_modules/ts-node/src/index.ts:1014:36)
    at Object.compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1322:43)
    at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1454:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:1458:12)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Function.Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19) {
  diagnosticCodes: [ 2307 ]
}
[nodemon] app crashed - waiting for file changes before starting...
zacharytyhacz commented 3 months ago

No idea, but i think it's because the package is only exported as esm

117 commented 3 months ago

Hmm. I'm unable to recreate this. I'm seeing the types inferred.

CleanShot 2024-06-19 at 06 34 24@2x

npm-test % tsc -v
Version 5.4.5
npm-test % node -v
v20.14.0
// package.json
{
  "type": "module",
  "dependencies": {
    "@alpacahq/typescript-sdk": "0.0.29-preview",
    "ts-node": "^10.9.2"
  },
  "devDependencies": {
    "typescript": "^5.4.5"
  }
}
// tsconfig.json
{
    "compilerOptions": {     
      "module": "NodeNext",
      "target": "ES6",  
      "moduleResolution": "NodeNext"
    }   
}
// test.ts
import { createClient } from "@alpacahq/typescript-sdk";

const client = createClient({
  key: "my-paper-key",
  secret: "my-paper-secret",
});

client.getAccount().then(console.log);
client.getAsset({ symbol_or_asset_id: "AAPL" }).then(console.log);

What node version are you on? What ts-node version are you on?

zacharytyhacz commented 3 months ago

I was using nodemon latest version, but now Im using "ts-node": "^10.9.2", and still getting same error.

I am node version v20.8.0

typescript version 5.4.5

zacharytyhacz commented 3 months ago

I restarted my computer and started with fresh new project, minimal package.json and still getting it.

weird error when i attempt to run npx tsx test.ts

node:internal/errors:497
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/zact/Projects/politician-trader/test/node_modules/@alpacahq/typescript-sdk/package.json
    at __node_internal_captureLargerStackTrace (node:internal/errors:497:5)
    at new NodeError (node:internal/errors:406:5)
    at exportsNotFound (node:internal/modules/esm/resolve:268:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:542:13)
    at resolveExports (node:internal/modules/cjs/loader:547:36)
    at Module._findPath (node:internal/modules/cjs/loader:621:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1034:27)
    at resolve (/Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-DfubRCxM.cjs:1:3084)
    at resolveRequest (/Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-DfubRCxM.cjs:1:2618)
    at /Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-DfubRCxM.cjs:1:3400
    at m._resolveFilename (file:///Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-CFO5XQXL.mjs:1:832)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at <anonymous> (/Users/zact/Projects/politician-trader/test/test.ts:2:30)
    at Object.<anonymous> (/Users/zact/Projects/politician-trader/test/test.ts:10:65)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Object.transformer (/Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-DfubRCxM.cjs:2:823)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at cjsLoader (node:internal/modules/esm/translators:284:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:234:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
117 commented 3 months ago

Try latest npm i @alpacahq/typescript-sdk@0.0.30-preview out.

zacharytyhacz commented 3 months ago

Try latest npm i @alpacahq/typescript-sdk@0.0.30-preview out.

ok I tested, still same error 🫤

117 commented 3 months ago

I was able to get it working with your exact configuration. Try latest again 😄.

npm i @alpacahq/typescript-sdk@0.0.31-preview

zacharytyhacz commented 3 months ago

I was able to get it working with your exact configuration. Try latest again 😄.

npm i @alpacahq/typescript-sdk@0.0.31-preview

I'm trying a ton of different things, but still same error. I am not sure what is happening brother, but still not working.

> npx --version
10.1.0
> npx tsx --version
tsx v4.15.6
node v20.8.0

package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc test.ts"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@alpacahq/typescript-sdk": "^0.0.31-preview",
    "ts-node": "^10.9.2",
    "typescript": "^5.4.5"
  }
}

ts config

{
    "compilerOptions": {
        "module": "ESNext",
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "target": "esnext",
        "noImplicitAny": true,
        "noEmit": true,
        "moduleResolution": "NodeNext",
        "sourceMap": true,
        "baseUrl": ".",
        "forceConsistentCasingInFileNames": true,
        "types": [
            "node"
        ]
    },
    "exclude": [
        "./node_modules"
    ]
}

same error

$ npx tsx test.ts | pbcopy
node:internal/errors:497
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/zact/Projects/politician-trader/test/node_modules/@alpacahq/typescript-sdk/package.json
    at __node_internal_captureLargerStackTrace (node:internal/errors:497:5)
    at new NodeError (node:internal/errors:406:5)
    at exportsNotFound (node:internal/modules/esm/resolve:268:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:542:13)
    at resolveExports (node:internal/modules/cjs/loader:547:36)
    at Module._findPath (node:internal/modules/cjs/loader:621:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1034:27)
    at resolve (/Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-DfubRCxM.cjs:1:3084)
    at resolveRequest (/Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-DfubRCxM.cjs:1:2618)
    at /Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-DfubRCxM.cjs:1:3400
    at m._resolveFilename (file:///Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-CFO5XQXL.mjs:1:832)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at <anonymous> (/Users/zact/Projects/politician-trader/test/test.ts:1:30)
    at Object.<anonymous> (/Users/zact/Projects/politician-trader/test/test.ts:9:65)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Object.transformer (/Users/zact/.npm/_npx/fd45a72a545557e9/node_modules/tsx/dist/register-DfubRCxM.cjs:2:823)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at cjsLoader (node:internal/modules/esm/translators:284:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:234:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12) {
  code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}

Node.js v20.8.0
majorsauce commented 2 months ago

@zacharytyhacz have you figured it out already ?

I came to the same issue and managed to figure out that my tsconfig.json was misconfigured by manually transpiling using the "tsc" command and inspecting the generated JS which contained a require statement instead of the import statement so it was evident that the tsconfig was somehow off. Once that was resolved, npx tsc would run perfectly fine. Still having some struggles with ts-node-dev/ts-node though.

zacharytyhacz commented 2 months ago

I did not, I just switched to @alpacahq/alpaca-trade-api 🫤

Al0ngsy commented 1 month ago

For the Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in ... problem, make sure that your package.json contains the "type": "module" line.

For the Cannot find module '@alpacahq/typescript-sdk' or its corresponding type declarations. problem, make sure your tsconfig.json contains the "esModuleInterop": true line inside the compilerOptions.

And from my testing nodemon and ts-node did not really work, try tsx (link) instead.