# how to import a javascript class

**URL:** <https://community.smartbear.com/t/how-to-import-a-javascript-class/308>\
**Category:** TestComplete\
**Tags:** test, testcomplete, migrated\
**Created:** [September 1, 2017, 1:29pm UTC](https://community.smartbear.com/t/how-to-import-a-javascript-class/308 "2017-09-01T13:29:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [September 1, 2017, 1:29pm UTC](https://community.smartbear.com/t/how-to-import-a-javascript-class/308/1 "2017-09-01T13:29:56Z")

</div>

> Originally posted by **jfk** on September 01, 2017

hi,

I would like to import a javascript class to another file but unfortunately I couldn’t successfully do that 🙂

I tried something like this:

```auto
// fileA
class A{
    constructor(x){
      this.x = x
}
    getX(){
      return this.x
 }
}
module.exports.class = A;

//fileB
var ss = require("fileA");

var tt = new ss.A("2")

```

but this doesn’t work. Couldn’t someone help me out ? 🙂

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [September 1, 2017, 1:44pm UTC](https://community.smartbear.com/t/how-to-import-a-javascript-class/308/2 "2017-09-01T13:44:06Z")

</div>

> Originally posted by **tristaanogre** on September 01, 2017

Your syntax is a bit off. When you do **modules.exports.class = A;** you’re basically saying that the class “A” is exported to other modules as “class”. Try the following and see if it works (worked for me).

```auto
// fileA
class A{
    constructor(x){
      this.x = x
}
    getX(){
      return this.x
 }
}
module.exports.A = A;

//fileB
var ss = require("fileA");

var tt = new ss.A("2")

```

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [September 1, 2017, 2:07pm UTC](https://community.smartbear.com/t/how-to-import-a-javascript-class/308/3 "2017-09-01T14:07:39Z")

</div>

> Originally posted by **jfk** on September 01, 2017

thank you

---

<div class="post-metadata">

**Author:** ![system](https://avatars.discourse-cdn.com/v4/letter/s/bcef8e/32.png) [@system](https://community.smartbear.com/u/system)\
**Post date:** [October 7, 2026, 5:14pm UTC](https://community.smartbear.com/t/how-to-import-a-javascript-class/308/4 "2026-10-07T17:14:47Z")

</div>

Closed on October 7, 2026, 5:14pm UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
