DanT29
DanT29

Reputation: 3229

fs.js module in nodejs requiring itself?

I'm currently learning nodeJS and running version v6.10.2, I was taking a look at the fs.js module source code located in the graceful-js folder and noticed this code in it:

'use strict'

var fs = require('fs')

module.exports = clone(fs)

function clone (obj) {
  if (obj === null || typeof obj !== 'object')
    return obj

  if (obj instanceof Object)
    var copy = { __proto__: obj.__proto__ }
  else
    var copy = Object.create(null)

  Object.getOwnPropertyNames(obj).forEach(function (key) {
    Object.defineProperty(copy, key, Object.getOwnPropertyDescriptor(obj, key))
  })

  return copy
}

How can a module require itself does it have to do with the clone keyword? I couldn't find any other module named fs.js either.

Upvotes: 0

Views: 227

Answers (1)

hopper
hopper

Reputation: 13390

You're looking at part of the code for the graceful-fs package which is a wrapper around the builtin fs module in NodeJS. So this code is not requiring itself - the require statement imports the builtin fs module and the rest of the code clones it so that other parts of the graceful-fs package can override some of the default fs functionality without affecting the original module.

Upvotes: 1

Related Questions