TypeError: Cannot read properties of undefined (reading 'startsWith')
Getting this error trying to use this library in my toy project:
TypeError: Cannot read properties of undefined (reading 'startsWith')
at getCallerDirname (/Users/chad/src/tally/node_modules/.pnpm/[email protected]/node_modules/import-sync/dist/cjs/files.js:27:50)
at Module.importSync (/Users/chad/src/tally/node_modules/.pnpm/[email protected]/node_modules/import-sync/dist/cjs/import.js:41:108)
at loadConfigFromFile (/Users/chad/src/tally/packages/typedconfig/src/load.ts:66:41)
at loadAllConfigFiles (/Users/chad/src/tally/packages/typedconfig/src/load.ts:19:8)
at Module.loadConfig (/Users/chad/src/tally/packages/typedconfig/src/load.ts:12:16)
at eval (/Users/chad/src/tally/apps/web/app/config.ts:12:43)
at async instantiateModule (file:///Users/chad/src/tally/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/vite/dist/node/chunks/dep-DG6Lorbi.js:52914:5)
Under debug, it looks like stack[1].getFileName(); is returning undefined but stack[1].getEvalOrigin() returns /Users/chad/src/tally/packages/typedconfig/src/load.ts (this is probably what we want here?)
I don't know why getFileName() is returning undefined. I'm using remix so maybe it has something to do with it being bundled?
Hi @chadxz,
Which package are you trying to import specifically? Or is it any package in general under your current environment/setup?
As an aside, I would also suggest looking into
- https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require
if you are on the latest node version.
I'm trying to import a file in my project, i.e. https://github.com/chadxz/tally/blob/add805d165f26a1a2932587ee31cd869b75b9241/packages/typedconfig/src/load.ts#L41 would import https://github.com/chadxz/tally/blob/main/apps/web/config/default.ts
I can try using the built in support again. I'm using remix for the first time so there's a bunch of moving parts there I'm unfamiliar with. I think I will have to pass the experimental flag in a NODE_OPTIONS env var to remix-serve and vite.
Anyway, I understand this package will soon be unneeded so it's unlikely to get changes. I'll see what I can do. Thx
Something you could try is removing the extension from the path and see if it would be picked up, e.g.
function loadConfigFromFile(path: string): NestedConfig {
try {
return importSync(path.replace(/\.[^/.]+$/, ""));
} catch (e) {
return {};
}
}
and see if import-sync can pick it up successfully.
Another alternative is using dynamic imports and changing the function to be async, e.g.
- https://stackoverflow.com/a/58859327/22324694
I was trying to avoid the function being async due to a lack of support in my orm (drizzle) for async loading of config.
Upon further investigation, I realized the server-side bundling that remix is doing would not understand what import-sync is doing.
Still debating how to approach this. Thinking maybe "don't do that" is correct.
Is it possible to use a serialised format like JSON for the config?
Then you can do fs.readFileSync to load the config, for example, and use something like zod for validation/parsing.
Was trying to give develop-time syntax highlighting and code completion for the config, so JSON wouldn't work unless I generated some sort of JSON Schema from the Zod definition. I don't think I want that sort of build step in the mix.