axiomhq / axiom-js

Official language bindings and library extensions for Axiom
https://axiom.co
MIT License
97 stars 14 forks source link

Unable to get axiomhq/js working in Vite #50

Closed BradNut closed 1 year ago

BradNut commented 1 year ago

In SvelteKit Vite I previously had @axiomhq/axiom-node set up and working.

Now after upgrading to use @axiomhq/js I get the following error:

11:55:43 PM [vite] Error when evaluating SSR module /src/lib/server/log.ts: failed to import "@axiomhq/js"
|- /home/name/project/node_modules/.pnpm/@axiomhq+js@0.1.2/node_modules/@axiomhq/js/dist/esm/index.js:1
export * from './client';
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)

Code for setup in Vite was the same as this open source project's file: https://github.com/delay/sveltekit-auth-starter/blob/main/src/lib/server/log.ts

The only changes were the import from import { Client } from '@axiomhq/axiom-node'; to import { Axiom } from '@axiomhq/js';

And from:

const client = new Client({
  token: AXIOM_TOKEN,
  orgId: AXIOM_ORG_ID
});
await client.ingestEvents(AXIOM_DATASET, [logData]);

To:

const axiom = new Axiom({
  token: AXIOM_TOKEN,
  orgId: AXIOM_ORG_ID
});
await client.ingest(AXIOM_DATASET, [logData]);
dasfmi commented 1 year ago

created a PR to switch to named exports, waiting for the ci workflow to confirm that its working, if so, will create a new release.

dasfmi commented 1 year ago

a new version is out with the fix from the PR above, can give it a try, hopefully it will work out ✌️

PandelisZ commented 1 year ago

I think the issue i'm facing is related, also had axiom node and switch to the latest patched verion 0.1.3 of winston but get this error

lient:build:
client:build: ./worker/lib/logger.ts:2:0
client:build: Module not found: Default condition should be last one
client:build:   1 | import winston from 'winston'
client:build: > 2 | import { WinstonTransport as AxiomTransport } from '@axiomhq/winston'
client:build:   3 | import env from '../../lib/env'
client:build:   4 | const { combine, errors, json } = winston.format

downgrading to 0.1.2 (before you patched for this issue, fixed it)

dasfmi commented 1 year ago

I think the issue i'm facing is related, also had axiom node and switch to the latest patched verion 0.1.3 of winston but get this error

lient:build:
client:build: ./worker/lib/logger.ts:2:0
client:build: Module not found: Default condition should be last one
client:build:   1 | import winston from 'winston'
client:build: > 2 | import { WinstonTransport as AxiomTransport } from '@axiomhq/winston'
client:build:   3 | import env from '../../lib/env'
client:build:   4 | const { combine, errors, json } = winston.format

downgrading to 0.1.2 (before you patched for this issue, fixed it)

that makes it more complex, I will have to look for a different way to compile to ESM And CJS.

PandelisZ commented 1 year ago

I just looked through the codebase looks like you're using tsc directly, I've had the same headache just the other month when i got into writing an itnernal lib.

What i've found works best is rollup. Seems the most reliable for creating types and esm/umd and commonjs packages all in 1.

It seems to be the standard across allot of libs. I've exprimented and now use esbuild for our internal things, but esbuild doesnt produce types so i still have to run tsc afterwards. So i'd recommend rollup for your use case to get the lib compatible with all 3 crazy javascript ecosystem types :P

https://dev.to/remshams/rolling-up-a-multi-module-system-esm-cjs-compatible-npm-library-with-typescript-and-babel-3gjg

I'd be happy to contribute a PR :) Just busy at the moment, but i'll try perhaps to maybe submit something next week.

Many thanks :)

dasfmi commented 1 year ago

hi @PandelisZ , any progress on that with rollup? I played a bit with converting the package to a module but had similar problems. If you have started something with rollup I can take a look and help pushing it to finish line.

dasfmi commented 1 year ago

I have something in progress here feel free to review and drop comments.

dasfmi commented 1 year ago

should be fixed, feel free to open again if you are still facing the same issue.