patmood / pocketbase-typegen

Typescript generation for pocketbase records
https://www.npmjs.com/package/pocketbase-typegen
512 stars 16 forks source link

Error when importing, pocketbase-types.js not found #93

Closed ramadhanafif closed 9 months ago

ramadhanafif commented 9 months ago

Hi, I still new in TS and JS, so please let me know what I might have missed.

When I import Collections from pocketbase-types as such

import {
  ClassTopicsResponse,
  Collections,
  TeachersResponse,
  TypedPocketBase,
} from "./pocketbase-types";

the compiled js code is as such

import { Collections, } from "./pocketbase-types";

which should be no problem right? But when I tried to run node with the generated .js file:

node:internal/modules/esm/resolve:255
    throw new ERR_MODULE_NOT_FOUND(
          ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/ramad/Documents/project/dist/pocketbase-types' imported from /home/ramad/Documents/project/dist/main.js
    at finalizeResolution (node:internal/modules/esm/resolve:255:11)
    at moduleResolve (node:internal/modules/esm/resolve:908:10)
    at defaultResolve (node:internal/modules/esm/resolve:1121:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///home/ramad/Documents/project/dist/pocketbase-types'
}

And the error goes away when I add extension to the js import:

import { Collections, } from "./pocketbase-types.js";

Can someone tell me why it behaves this way? I never encountered such error when omitting extension to the import in javascript.

patmood commented 9 months ago

I think this means your node project is using ECMAScript modules (ESM) instead of CommonJS. If you have “type”: “module” in package.json that will use ESM and require the .js extension for imports.

How is your project being built? Is the target node or browser js?

On Sun, 31 Dec 2023 at 2:57 am, Muhammad Afif Ramadhan < @.***> wrote:

Hi, I still new in TS and JS, so please let me know what I might have missed.

When I import Collections from pocketbase-types as such

import { ClassTopicsResponse, Collections, TeachersResponse, TypedPocketBase,} from "./pocketbase-types";

the compiled js code is as such

import { Collections, } from "./pocketbase-types";

which should be no problem right? But when I tried to run node with the generated .js file:

node:internal/modules/esm/resolve:255 throw new ERR_MODULE_NOT_FOUND( ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/ramad/Documents/project/dist/pocketbase-types' imported from /home/ramad/Documents/project/dist/main.js at finalizeResolution (node:internal/modules/esm/resolve:255:11) at moduleResolve (node:internal/modules/esm/resolve:908:10) at defaultResolve (node:internal/modules/esm/resolve:1121:11) at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12) at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25) at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38) at ModuleWrap. (node:internal/modules/esm/module_job:85:39) at link (node:internal/modules/esm/module_job:84:36) { code: 'ERR_MODULE_NOT_FOUND', url: 'file:///home/ramad/Documents/project/dist/pocketbase-types' }

And the error goes away when I add extension to the js import:

import { Collections, } from "./pocketbase-types.js";

Can someone tell me why it behaves this way? I never encountered such error when omitting extension to the import in javascript.

— Reply to this email directly, view it on GitHub https://github.com/patmood/pocketbase-typegen/issues/93, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA6YGPFZBO57NTV67S2UBNLYMFAJXAVCNFSM6AAAAABBIFQJ42VHI2DSMVQWIX3LMV43ASLTON2WKOZSGA3DCMBSGE2TOMA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

ramadhanafif commented 9 months ago

It is a node application, and expected to run as a backend program. I expect to have it writen in typescript and compiled to js for the production.

I think you're right, it is a typescript issue as stated here

Thanks for pointing out the ESM vs CJS!

Current solution for this issue is setting tsconfig.json

    "module": "NodeNext"
    "moduleResolution": "NodeNext",

and then adding .js to the file extension in the typescript file like so

import {
  ClassTopicsResponse,
  Collections,
  TeachersResponse,
  TypedPocketBase,
} from "./pocketbase-types.js";