menphis
menphis

Reputation: 95

Export a class in Typescript and Node JS

I'm creating a class in a separate file to import from the controller, but it won't let me declare variables inside the class.

This is the class I want to export:

const fs = require("fs");
const rimraf = require("rimraf");

export class createDir {
    let dir = 'src/output';
    if (fs.existsSync(dir)) {
        rimraf.sync(dir);
    }else {
        fs.mkdirSync(dir);
     }
}

This is the error that let shows:

Unexpected token. A constructor, method, accessor, or property was expected

My problem: how can I export the function in Typescript and what is the problem?

Upvotes: 1

Views: 273

Answers (1)

Frenchiejnr
Frenchiejnr

Reputation: 326

You need a function in your class:

export class createDir {
  function checkExistsOrCreate () {
    let dir = 'src/output';
    if (fs.existsSync(dir)) {
        rimraf.sync(dir);
    }else {
        fs.mkdirSync(dir);
     } 
  }
}

Upvotes: 2

Related Questions