#Exported variable from file is undefined when compiled to NodeJS.

48 messages · Page 1 of 1 (latest)

sly iris
#

Hello! I've been struggling with some 'bug' that I found with typescript, I have two files:

// module.ts
export const Human = {
  name: "Samy", 
  age: 20
}

// index.ts
import { Human } from './module.ts'

console.log(Human) // undefined

// Note: TS checking marks it correct, but when I compile to NodeJS and run it, it says that that: require('./module.js') will give { Human: undefined }

I'm struggling please help 😂

#

tsconfig.json:

weak crater
#

you just posted this, someone will help you in a while. If not you can use !helper, see #how-to-get-help. Please don't cross-post in #random

grave hull
#

compiling to cjs?

sly iris
#
{
  "compilerOptions": {
    "target": "ES2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
    "module": "commonjs" /* Specify what module code is generated. */,
    "outDir": "./dist" /* Specify an output folder for all emitted files. */,
    "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
    "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,

    "strict": true /* Enable all strict type-checking options. */,
    "skipLibCheck": true /* Skip type checking all .d.ts files. */
  }
}
sly iris
mossy gust
#

There isn't a circular reference?

sly iris
#

I don't think so, everything is fine

#

that sooo strange

grave hull
sly iris
#

yea it's reduced

#

the code is huge I can't really send it

grave hull
#

have you tried reducing the code to a mcve?

sly iris
#

you mean reduce it as many as possible ?

#

i'll try

#

oh wait there might be a circular reference

grave hull
sly iris
#

main file imports module => module imports module2 => module2 imports module => ...

#

but the code was working fine i don't think that's the issue

mossy gust
#

I'd guess it is something with the circular reference.

sly iris
#

is there a collaborator that could maybe join :D ? <3

#

I'd be really really thankful

#

it's a programming lanugage that I'm making, and I've been stuck here for 2 days

grave hull
#

join what exactly?

sly iris
#

VSCode live share session

grave hull
#

have you reduced the code to the part causing problems?

sly iris
#

I'll try but it's gonna take some time

#

can a circular reference still be considered a circular reference if it only imports types ?

#

cuz when compiling to cjs it will not even try to import, so it cancels out

mossy gust
#

Yeah, type-only circular references are removed before the code runs so they don't matter.

sly iris
#

yea and that justifies that circular reference isn't the problem (as the code was successively running 2 days ago)

mossy gust
#

I mean, if the code is unchanged it should still be running today.

#

Presumably you changed something - I think certain circular reference patterns are okay, some are not.

#

(e.g. it's okay for functions to circular reference each other as long as they are both setup before they're called)

sly iris
#

whoever who joins me on vs to fix this will get a free course on how to make a programming language 🥲

#

I just can't get it fixed

grave hull
#

...have you made an mcve yet?

sly iris
#

I can't sorry

#

I still have the repo

#

here, exclude FNVal, NativeFNVal, ObjectValue, RuntimeValue as they're only Types.

#

now when compiled: when trying to use MK it says MK is undefined (module_1.MK) while it's well defined in that module file

#

anything that is exported standalone in that file will result in undefined