Avoid using relative paths on require()

前端 未结 2 1772
情话喂你
情话喂你 2021-01-26 06:57

I\'m new with NodeJs, my project is growing and i dont want make relative imports, i want imports like these installed from npm.

I have the following directory structure

相关标签:
2条回答
  • 2021-01-26 07:29

    This topic is extensively discussed here. My conclusion is mostly:

    1. The idea of putting tests in a separate directory is ineffective. Put tests right alongside the main code: app/users/user.js and app/users/user.test.js.
    2. I create a symlink at node_modules/app that points to ../app so I can require('app/users'); from anywhere in the code base.
    0 讨论(0)
  • 2021-01-26 07:33

    There is nothing which says you cannot put your own code inside node_modules. This will enable you to reference it as you want. If you have a .gitignore or equivalent preventing code inside node_modules from being committed, you can add exceptions for your own code.

    node_modules
    !node_modules/model/
    
    0 讨论(0)
提交回复
热议问题