简体   繁体   English

Node.js:在文件夹结构中放置内部模块的位置?

[英]Node.js: Where To Place Internal Modules In Folder Structure?

The Situation 情况

I often see Node.js applications with the following structure: 我经常看到具有以下结构的Node.js应用程序:

Common pattern: 常见模式:

  • lib/ or src/ - the self-written code lib/src/ - 自编代码
    • index.js - main code index.js - 主要代码
    • internal modules ... (eg self-written for this project) 内部模块 ...(例如,为此项目自编)
  • node_modules
    • external modules ... (eg taken from another project) 外部模块 ......(例如从另一个项目中获取)
  • package.json

My Problem 我的问题

What I don't like about this pattern: 我不喜欢这种模式:

  • I don't feel comfortable about it because you have to explicitly specify the directory path of the internal modules when require() ing: 对此感到不舒服 ,因为在require()时你必须明确指定内部模块的目录路径

     // /lib/index.js var internalMod = require('./internal'); // `require('internal')` (without path) wouldn't work internalMod.doSomething(); 

My Idea 我的点子

So I think it would be a good idea also to place internal modules in an node_modules folder (somewhere in the project). 所以我认为将内部模块放在node_modules文件夹(项目中的某个位置)也是一个好主意。 So node would be able to find them, even if you don't explicitly specify the path. 因此即使您没有明确指定路径, node也能够找到它们。

For example: 例如:

  • src/ - the self-written code src/ - 自编代码
    • index.js - main code index.js - 主要代码
    • node-modules - for internals node-modules - 用于内部
      • internal modules ... 内部模块 ......
  • node_modules - for externals node_modules - 用于外部
    • external modules ... (eg taken from another project) 外部模块 ......(例如从另一个项目中获取)
  • package.json

My Question 我的问题

  1. Are there any cons about my plan? 关于我的计划是否有任何缺点?
  2. Is there another idea where to place internal modules in folder structure? 还有其他想法将内部模块放在文件夹结构中吗?

Thanks for your answer (or comment). 谢谢你的回答(或评论)。 - If anything is unclear, please comment. - 如果有任何不清楚的地方,请发表评论。

Perhaps you could use npm link to pull your modules into node_modules? 也许您可以使用npm link将模块拉入node_modules? See: https://docs.npmjs.com/cli/link 请参阅: https//docs.npmjs.com/cli/link

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM