2

I'm having a really weird issue with importing my class into another module. In my start.js file, I can import the worker module and it runs the script fine. However, when it gets to the socket module and tries to import the worker module, I get the following error.

When I console log worker in the socket.js constructor, it shows as an empty object. When I console log it in the start.js, it shows as a function.

I know this has to be something very simple that I'm overlooking, but I just can't see it.

this.Worker = new worker()
             ^
TypeError: worker is not a constructor
    at new Socket (C:\Users\***\Desktop\***\src\modules\socket.js:7:17)
    at new Worker (C:\Users\***\Desktop\***\src\modules\worker.js:6:17)
    at Object.<anonymous> (C:\Users\***\Desktop\***\start.js:2:16)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)

I have a start.js file where I import my worker module and start my app.

const worker = require('./src/modules/worker')
const Worker = new worker()

Worker.start()

The worker module (worker.js) imports my socket server module and connects to the socket server.

const socket = require('./socket')

class Worker {

    constructor() {
        this.Socket = new socket()
    }

    async start() {

        try {

            console.log('ran start')
            // Connect to socket server
            await this.Socket.connect()

        } catch(err) {
            console.log('Unable to connect to socket server.', err)
        }
    }

    startJob(work) {
        const jobType = work.work.jobType
        const jobData = work.work.jobData

        // run module based on jobType
    }
}

module.exports = Worker

My socket server module (socket.js) also imports (worker.js) so that I can use the startJob function within the socket.on('process') event as Worker.startJob().

const worker = require('./worker');

class Socket {

    constructor() {
        this.socket = require('socket.io-client')('http://localhost:3000')
        this.Worker = new worker()
    }

    async connect() {

        try {

            // Connect to socket server
            await this.connectServer()

        } catch(err) {
            console.log('Unable to start worker.', err)
        }
    }

    async connectServer() {
        let workerId;

        try {

            // Connection to socket server established
            this.socket.on('connect', () => console.log('Connected to socket server.'))

            // Get socketId for job issuance
            this.socket.on('id', id => { workerId = id })

            // Receive new work orders
            this.socket.on('process', async work => {
                const myId = work.id

                // Check if job was assigned to my socketId
                if (myId == workerId) {
                    console.log('starting job')
                    // await this.Worker.startJob(work)
                }
            })

        } catch(err) {
            console.log('Unable to connect to socket server.', err)
        }
    }
}

module.exports = Socket

2 Answers 2

4

There are couple of things to note with modules and using export/import

The module.exports object is created by the Module system

So when you do, module.exports = Worker, Worker is added to the module.export object meaning it's a key in the object.

Where you require in your code:

const worker = require('./src/modules/worker')
// class Worker is available as a member
const Worker = new worker.Worker()

If you don't want this, there's two other way you could do:

Destructuring assignment

// note the curly braces and capitalized W because JavaScript is case sensitive
const { Worker } = require('./src/modules/worker');

Or you can replace module.exports with your class this way:

module.exports = class {

  constructor() {
    this.Socket = new socket()
  }

  async start() {

    try {

      console.log('ran start')
      // Connect to socket server
      await this.Socket.connect()

    } catch(err) {
      console.log('Unable to connect to socket server.', err)
    }
  }

  startJob(work) {
    const jobType = work.work.jobType
    const jobData = work.work.jobData

    // run module based on jobType
    }
}

And import like: (Note, You can name it to anything else)

const Worker = require('./worker')
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly what I was looking for, thank you for the detailed reply!
0

The error is triggered because you don't have a worker class you have a Worker class (it's case-sensitive).

1 Comment

I'm importing it as const worker though and setting this.Worker = new worker(). Just like I did in start.js. const worker = require('...'), this.Worker = new worker()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.