ts-node import not defined at runtime

1.8k views Asked by At

I have the following reference import { STORE } from "../data/store"; It's part of a react project and works as expected.

However I need to run some code separately over STORE and accessed it the same way as it is in the react project, but ran via ts-node. When I try to access STORE by let data = STORE[videoId].labels;, with videoId set as "home" I get TypeError: Cannot read property 'home' of undefined.

Might anyone know what I'm missing --- must be something specific to ts-node...? Thanks!

2

There are 2 answers

1
TheNickyYo On

It sounds like the property STORE is undefined..

If you change import { STORE } from "../data/store"; to import * as STORE from "../data/store" does it work?

0
Zwyx On

In case someone arrives on this page because of imports issue, like I did, here's what my problem was:

I had three files : index.ts, File.json and File.ts

In index.ts:

import { thing } from "./File";

was working well in VS Code: I had auto-completion for thing and everything.

But when running ts-node index.ts, thing was undefined!

When I remembered that I had the json file, I just renamed File.ts to File.module.ts and changed the import to:

import { thing } from "./File.module";

and it was sorted.