0

UPDATE:

Even with ts-node --files node_modules/typescript/lib/lib.d.ts ./lib/foo.ts I'm still unable to get this to work.

ORIGINAL:

I have this simple file called foo.ts that I'm trying to process with ts-node.

export class foo extends HTMLElement {
    hello() {
        console.log("hello")
    }
}

const f = new foo();

f.hello();

When I rung ts-node ./lib/foo.ts the library returns the following error:

/Users/haroldcampbell/work/go/src/groundtap/library/lib/foo.ts:1
export class foo extends HTMLElement {
                         ^
ReferenceError: HTMLElement is not defined
    at Object.<anonymous> (/Users/haroldcampbell/work/go/src/groundtap/library/lib/foo.ts:1:26)
    at Module._compile (internal/modules/cjs/loader.js:1201:30)
    // truncated for brevity...
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at main (/usr/local/lib/node_modules/ts-node/src/bin.ts:198:14)
    at Object.<anonymous> (/usr/local/lib/node_modules/ts-node/src/bin.ts:288:3)

The challenge is that it can't locate the typescript bindings which are in the node_modules/typescript/lib/lib.d.ts file

I've tried various other version of calling ts-node with no luck. For example:

  • ts-node -r node_modules/typescript/lib/lib.d.ts ./lib/foo.ts
  • npx ts-node -r node_modules/typescript/lib/lib.d.ts ./lib/foo.ts

Interestingly, Webpack doesn't have a problem processing the ./lib/foo.ts file, and does so without errors.

This is my tsconfig.json file:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2016",
        "sourceMap": true,
        "allowJs": true,
        "strictNullChecks": true,
        "strict": true,
        "noImplicitAny": true,
        "outDir": "./www/assets/dist/",
        "lib": [
            "es2016",
            "dom"
        ]
    },
    "exclude": [
        "node_modules",
        "**/node_modules/*"
    ],
}

Any assistance in getting this to work would be greatly appreciated.

Ultimately, I want to be able to convert my .js substack/tape test to .ts files and run tape tests written with typescript on my typescript project files like so:

ts-node node_modules/tape/bin/tape ./lib/foo.test.ts
haroldcampbell
  • 1,512
  • 1
  • 14
  • 22
  • This looks like a runtime error, not a compile-time one. Why do you expect `HTMLElement` to exist in Node.js environment at all? – Cerberus Mar 05 '21 at 19:35
  • Hi @Cerberus, the HTMLElement is a part of typescript. This is pulled in without a problem in Webpack and compiles ok with regular tsc. I'm unsure how to do something similar with ts-node. Do you have a recommendation? – haroldcampbell Mar 05 '21 at 19:56
  • 1
    HTMLElement is a part of browser DOM API, it doesn't exist in Node.js by default. If you want to run browser-targeted code on Node.js, you need something like [jsdom](https://www.npmjs.com/package/jsdom). – Cerberus Mar 06 '21 at 09:19
  • Thanks @Cerberus. This makes sense now that you have highlighted it. If you add your comment as an answer I'll mark it as the correct answer. – haroldcampbell Mar 07 '21 at 11:49

0 Answers0