Home > OS >  Is Node a predefined class in Node.js?
Is Node a predefined class in Node.js?

Time:07-17

I am learning Linear Data Structures in JavaScript via Codecademy and in lesson's introduction it states:

We are going to use a provided Node class, which you can find in Node.js.

The code in their built-in IDE is as follows:

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

class LinkedList {
  
}

module.exports = LinkedList;

When you look at the Node.js file referenced in require, there is a Node class defined like so:

class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }

  setNextNode(node) {
    if (!(node instanceof Node)) {
      throw new Error('Next node must be a member of the Node class');
    }
    this.next = node;
  }

  getNextNode() {
    return this.next;
  }
}

module.exports = Node;

So basically, does this already exist in a Node.js module to avoid having to access them all for this lesson, or did they create a custom class named Node?

CodePudding user response:

The require('./Node'); indicates where the dependency came from.

I would recommend https://nodejs.dev/learn/nodejs-file-paths for more info on file path access.

As provided in your example, it seems that the file (Node.js) can be found in the project's root directory, and it is 100% their implementation of the Node class as Node.js does not provide 'default' classes and implementations.

If you cannot use the same project as they did, then I suppose you will not have access to the Node class. You can make such folder structures; to have a shared folder library along with other folders per class/lesson, which will inject/require such shared dependencies.

  • Related