1

A number of packages on npm seem to use require function in the following way:

var CounterActions = require('../actions/counter');

instead of this:

var CounterActions = require('mypackage/actions/counter');

Why is that? I can see no upsides, and it makes it very hard to move the file to another location. Plus it is harder to read the code since you don't know what the require is referring to (e.g. require("../../../../../../index.js"))

Adam Zielinski
  • 2,774
  • 1
  • 25
  • 36

1 Answers1

1

Because you can't require something that is not in node_modules folder without a relative path.

The format : var CounterActions = require('mypackage'); is reserved for package in node_modules folder.

Some people have implemented NPM module to overcome this. Here is one of them : rootpath

There are many other solutions that you can find here, but NPM module seems to be the simplest.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
Louis Barranqueiro
  • 10,058
  • 6
  • 42
  • 52
  • 1
    There are some ways to do it though: https://stackoverflow.com/questions/10860244/how-to-make-the-require-in-node-js-to-be-always-relative-to-the-root-folder-of-t – Adam Zielinski Nov 17 '15 at 21:51
  • 1
    Then the answer is simple. The majority don't feel it's worth the effort to install yet another module to make it possible to do it the way you want (or don't know that it is even possible or care) – Kevin B Nov 17 '15 at 22:08