[英]Node.js: Where To Place Internal Modules In Folder Structure?
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
- 主要代码 node_modules
package.json
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();
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
- 用于内部
node_modules
- for externals node_modules
- 用于外部
package.json
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.