#Trying to make an NPM library, but methods and classes are all undefined.
1 messages · Page 1 of 1 (latest)
package.json
{
"name": "libname",
"version": "0.0.1",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "tsc",
"local": "tsc && npm pack --pack-destination ~"
},
"devDependencies": {
"typescript": "^4.9.5"
},
"type": "module"
}
The library doesn't seem to need a typescript dependency? I don't want any dependencies if I don't need them.
tsconfig.json
{
"compilerOptions": {
"module": "es2020",
"target": "es2020",
"declaration": true,
"outDir": "./dist",
"esModuleInterop": true
},
"include": [
"src/**/*"
]
}
index.ts
export * from './test'
export * from './components/meta'
export * from './lib/utils'
test.ts
export function sayHello() {
console.log('hi')
}
This is my first npm library, so I feel likeI'm missing something really basic that hard to search about.
TypeError: Cannot read properties of undefined (reading 'sayHello')
what code causes this exactly? do you have a stacktrace?
I do, it's initially being run SSR side of a SvelteKit app, so basically node.js
TypeError: Cannot read properties of undefined (reading 'sayHello')
at load (/....../src/routes/+layout.ts:15:2)
at Module.load_data (/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:110:43)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async eval (/node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:196:13)
It's SSR, I want to support both node and browser. That may be the source of my issues.
maybe try running it in a minimal environment first
minimal in browser? not node?
could be in node
so the simplest project with the depndency:
test.js
import { sayHello } from 'libname'
sayHello()
node test.js gets this error:
node:internal/process/esm_loader:108
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/..../test/node_modules/.pnpm/file+..+..+..+libname-0.0.1.tgz/node_modules/libname/dist/test' imported from /..../test/node_modules/.pnpm/file+..+..+..+libname-0.0.1.tgz/node_modules/libname/dist/index.js
Did you mean to import ../../../../../../../test.js?
at new NodeError (node:internal/errors:400:5)
at finalizeResolution (node:internal/modules/esm/resolve:260:11)
at moduleResolve (node:internal/modules/esm/resolve:879:10)
at defaultResolve (node:internal/modules/esm/resolve:1087:11)
at nextResolve (node:internal/modules/esm/loader:161:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:834:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:415:18)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
at link (node:internal/modules/esm/module_job:76:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
not certain if that use is correct though
Does anybody have an example of the simplest working TypeScript npm library exporting basic Classes and functions?
Can u post ur folder structure?
what does your dist folder look like
here's the dist folder in my other project node_modules
im trying everything i can. I tried putting this right in the index.ts so not even importing:
export function sayHello() {
console.log('hi');
}
export const sayGoodbye = () => {
console.log('goodbye');
};
still undefined
this reddit sonuds similar but they dont seem to have figured it out:
https://www.reddit.com/r/typescript/comments/d0gxyx/why_is_my_class_undefined_when_exported_like_this/
4 votes and 13 comments so far on Reddit
OK. So I went back to scratch:
- I made the most simple javascript only with just a
index.jswith those methods. It worked. - I manually added a
index.d.tswith type defs for those methods. It worked. - I moved those methods into a seperate
test.jsandtest.d.tsand imported in index, and I got the error! methods undefined.
So the problem is with how I'm importing:
export * from './test';
This also fails with the same undefined methods:
export { sayHello, sayGoodbye } from './test';
those exports are fine though? I pinpointed the problem, but what could be wrong?
Do you have a repo I could look at? I suspect esm shenanigans, but it's hard to say for sure without a project to look at
do you perhaps need to import from ./test.js, not ./test?
@exotic minnow u will have to add the .js extension for ur imports, so it should look like
export { sayHello, sayGoodbye } from ‘./test.js';
I suspect this could be the issue, lemme know if it fixes
lol
ill try....
yeah! that worked with my testlib... ill try ith my main lib and more real situation...
does not work with typescript. this creates the error:
export * from './lib/utils.ts'
An import path cannot end with a '.ts' extension. Consider importing './lib/utils.js' instead.ts(2691)
should index.ts just import the .js files, even they they don't exist yet?
Yeah, TS doesn't rewrite exports, so your imports should try to import the extension that the files will eventually have
if so, should my index.ts just be index.js?
TS will still resolve the .js import to the .ts file for type checking, but it will point to the compiled file when emitted
Even if u have any typescript files ur import should still be .js
Typescript will be smart enough to know to look for .ts
Once the file is compiled it becomes .js so that’s why u gotta put that
Ur index can stay as .ts
I dono. it worked for the testlib with just those sayHello() methods. But the class in my main ts library is still undefined, same error as before.
thanks though, that .js stuff seems like correct thing...
or any method
still trying stuff.... it seems to be just the class that's breaking everything. When I exclude the class, the other utility methods work. When I include the class then everything is undefined.
that class has some static
Ur .js need to be added in every file that has imports
did that. i think it's something to do with a static class. trying to use a modue or namespace instead, but same undefined
Another way to check is try to debug and run ur compiled .js code in dist/
Add some codes in the index.js to see if things work
I think I got it working.... went back to class. I was storing a reference to window.fetch but works now that i check (typeof window !== 'undefined') first.
Doesn't make sense to me that it would cause the whole class to be undefined -- the same error as the .js thing. Dono, too tired. maybe make sense tomorrow.
It's all working. There were two issues causing the same error (making the imported class undefined). Neither intuitive:
- Library imports all need
.js. - Don't use
windowserver-side.