0

I am creating a node express application. For my use case, I have a 1.ts file with a class as:

export class blah {
    constructor(props) {

    }
    tt() {
      console.log('logging function');
    }
}

in my 2.js file i am importing as

const blah = require('./1')
var b = new blah.blah()
console.log(b.tt())

But nothing in console.function is not being called.

How do i fix this problem. tsconfig.json file content

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
   "sourceMap": true,
   "experimentalDecorators": true,
   "emitDecoratorMetadata": true
  },
 "exclude": [
"node_modules"
 ]
}
4
  • Your code works on my machine. Did you recompile with tsc 1.ts? Commented Feb 5, 2020 at 18:23
  • yes here is my package.json scripts "build-ts": "tsc && nodemon ./src/index.js", "dev": "nodemon -e ts --exec \"yarn build-ts\"" and i am running by yarn dev Commented Feb 5, 2020 at 18:25
  • Do you have a tsconfig.json file? Commented Feb 5, 2020 at 18:27
  • here it is.. updated the question. Commented Feb 5, 2020 at 18:29

2 Answers 2

1

It must be const blah = require("./1")

Sign up to request clarification or add additional context in comments.

4 Comments

You are using {} it's used for destruction of object..means when your library export multiple things and want to access part of it then you can use destruction syntax..
no its not working with your solution, its giving the same result.
updated the question, its recognising the class but not able to call the function.
Please add an explanation to your answer why you think it should look like that, and why the OP's code didn't work.
1

I am posting as an answer to post the contents of the files. This code works for me. I am running just the command tsc in the directory with the files. Output files are 1.js and 1.js.map. Then I run node 2. The output is

logging function
undefined

(On a sidenote: It prints undefined because the call console.log(b.tt()) tries to print the return value of b.tt(), but it does not return anything)

const blah = [...] gives me the same error you got.

Directory structure (everything on the same level):

  • 1.ts
  • 2.js
  • tsconfig.json

1.ts

export class blah {
    constructor(props) {

    }
    tt() {
      console.log('logging function');
    }
}

2.js

const {blah} = require('./1')
var b = new blah()
console.log(b.tt())

tsconfig.json

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "sourceMap": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    },
    "exclude": [
        "node_modules"
    ]
}

2 Comments

even if i add a return statement in the function its not printing anything in the console.
Did you try just copying the contents of the files I posted and following the steps to rule out a problem with the typescript compilation? Based on if it's working or not you could rule out some possibilities for error. Maybe there is a problem with `nodemon -e ts --exec \"yarn build-ts`

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.