Avoid using relative paths on demand ()

I'm new to NodeJs, my project is growing and I don't want to do relative imports, I want to import as installed from npm.

I have the following directory structure

Project node_modules / src / model / person.js testwork / model / person.js app.js config.js

I am using mongoose and I can require mongoose from anywhere with require('mongoose')

, but for example, if you want my user model from app.js

I write require('./model/person')

and from mine test/model/person.js

I need to write require('../../src/model/person')

.

Is there some way "native" (without installing additional dependencies) to do it like require('model/person')

from anywhere in my application?

0


source to share


2 answers


This section is discussed in detail here . My conclusion is basically:



  • The idea of ​​putting tests in a separate directory is ineffective. Place your tests right next to your main code: app/users/user.js

    and app/users/user.test.js

    .
  • I am creating a symbolic link in node_modules/app

    that points to ../app

    , so I can require('app/users');

    from anywhere in the code base.
+2


source


There is nothing that says that you cannot put your own code inside node_modules. This will allow you to link to it the way you want. If you have .gitignore or equivalent prevention code inside node_modules, you can add exceptions for your own code.



node_modules
!node_modules/model/

      

0


source







All Articles