Skip to content

Is it legal to modify module.paths? #2711

Closed
@hopeless-programmer-online

Description

  • Node.js Version:
    v.13.12.0
  • OS:
    Windows 7
  • Scope (install, code, runtime, meta, other?):
    Runtime/docs.
  • Module (and version) (if relevant):
    module.paths.

Docs are not very helpful, since they do not clarify is this property mutable or not.

It is important, because changing this variable actually leads to changes in the behavior of require function.
So it would be good if someone make it clear what kind of usage this property supports and intended to.

Basically, the following code works just fine:

const path = require(`path`);

// adding path to `lol` directory, which contains `kek.js` file
module.paths.unshift( path.join(__dirname, `lol`) );

// require works, despite `kek` is not inside of any of `node_modules` directories
const kek = require("kek");

console.log(kek);

But should it work at all?
It hard to say from the docs.

Please, advise. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions