文件和文件夹的 Node.js 项目命名约定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18927298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js project naming conventions for files & folders
提问by Rudiger
What are the naming conventions for files and folders in a large Node.js project?
大型 Node.js 项目中文件和文件夹的命名约定是什么?
Should I capitalize, camelCase, or under-score?
我应该大写、驼峰式还是下划线?
Ie. is this considered valid?
IE。这被认为是有效的吗?
project-name
app
controllers
someThings.js
users.js
models
someThing.js
user.js
views
some-things
index.jade
users
logIn.jade
signUp.jade
...
回答by bodokaiser
After some years with node, I can say that there are noconventions for the directory/file structure. However most (professional) express applications use a setup like:
使用 node 几年后,我可以说目录/文件结构没有约定。然而,大多数(专业)快递应用程序使用如下设置:
/
/bin - scripts, helpers, binaries
/lib - your application
/config - your configuration
/public - your public files
/test - your tests
An example which uses this setup is nodejs-starter.
使用此设置的一个示例是nodejs-starter。
I personally changed this setup to:
我个人将此设置更改为:
/
/etc - contains configuration
/app - front-end javascript files
/config - loads config
/models - loads models
/bin - helper scripts
/lib - back-end express files
/config - loads config to app.settings
/models - loads mongoose models
/routes - sets up app.get('..')...
/srv - contains public files
/usr - contains templates
/test - contains test files
In my opinion, the latter matches better with the Unix-style directory structure (whereas the former mixes this up a bit).
在我看来,后者与 Unix 风格的目录结构更匹配(而前者有点混淆)。
I also like this pattern to separate files:
我也喜欢这种分隔文件的模式:
lib/index.js
库/索引.js
var http = require('http');
var express = require('express');
var app = express();
app.server = http.createServer(app);
require('./config')(app);
require('./models')(app);
require('./routes')(app);
app.server.listen(app.settings.port);
module.exports = app;
lib/static/index.js
库/静态/index.js
var express = require('express');
module.exports = function(app) {
app.use(express.static(app.settings.static.path));
};
This allows decoupling neatly all source code without having to bother dependencies. A really good solution for fighting nasty Javascript. A real-world example is nearbywhich uses this setup.
这允许整齐地解耦所有源代码,而不必担心依赖关系。一个非常好的解决讨厌的 Javascript 的解决方案。附近有一个使用此设置的真实示例。
Update (filenames):
更新(文件名):
Regarding filenames most common are short, lowercasefilenames. If your file can only be described with two words most JavaScript projects use an underscore as the delimiter.
关于文件名,最常见的是短的、小写的文件名。如果您的文件只能用两个词来描述,那么大多数 JavaScript 项目都使用下划线作为分隔符。
Update (variables):
更新(变量):
Regarding variables, the same "rules" apply as for filenames. Prototypes or classes, however, should use camelCase.
关于变量,同样的“规则”适用于文件名。但是,原型或类应该使用camelCase。
Update (styleguides):
更新(风格指南):
回答by vaughan
Use kebab-casefor all package, folder and file names.
使用kebab-case所有的包,文件夹和文件名。
Why?
为什么?
You should imagine that any folder or file might be extracted to its own package some day. Packages cannot contain uppercase letters.
您应该想象有一天,任何文件夹或文件都可能被提取到自己的包中。包不能包含大写字母。
New packages must not have uppercase letters in the name. https://docs.npmjs.com/files/package.json#name
新包的名称中不得包含大写字母。 https://docs.npmjs.com/files/package.json#name
Therefore, camelCaseshould never be used. This leaves snake_caseand kebab-case.
因此,camelCase绝不应使用。这离开snake_case和kebab-case。
kebab-caseis by far the most common convention today. The only use of underscores is for internal node packages, and this is simply a convention from the early days.
kebab-case是迄今为止最常见的约定。下划线的唯一用途是用于内部节点包,这只是早期的约定。
回答by yitsushi
There are no conventions. There are some logical structure.
没有约定。有一些逻辑结构。
The only one thing that I can say: Never use camelCase file and directory names. Why? It works but on Mac and Windows there are no different between someAction and some action. I met this problem, and not once. I require'd a file like this:
我唯一能说的一件事是:永远不要使用驼峰式文件名和目录名。为什么?它可以工作,但在 Mac 和 Windows 上 someAction 和 some action 之间没有区别。我遇到了这个问题,而且不是一次。我需要一个这样的文件:
var isHidden = require('./lib/isHidden');
But sadly I created a file with full of lowercase: lib/ishidden.js. It worked for me on mac. It worked fine on mac of my co-worker. Tests run without errors. After deploy we got a huge error:
但遗憾的是我创建了一个全小写的文件:lib/ishidden.js. 它在 mac 上对我有用。它在我同事的 mac 上运行良好。测试运行没有错误。部署后,我们遇到了一个巨大的错误:
Error: Cannot find module './lib/isHidden'
Oh yeah. It's a linux box. So camelCase directory structure could be dangerous. It's enough for a colleague who is developing on Windows or Mac.
哦耶。这是一个Linux盒子。所以camelCase 目录结构可能是危险的。对于在 Windows 或 Mac 上开发的同事来说已经足够了。
So use underscore (_) or dash (-) separator if you need.
因此,如果需要,请使用下划线 (_) 或破折号 (-) 分隔符。
回答by Vlad Bezden
Based on 'Google JavaScript Style Guide'
File names must be all lowercase and may include underscores (_) or dashes (-), but no additional punctuation. Follow the convention that your project uses. Filenames' extension must be .js.
文件名必须全部小写,并且可以包含下划线 (_) 或破折号 (-),但不能包含额外的标点符号。遵循您的项目使用的约定。文件名的扩展名必须是 .js。
回答by Mathieu Amiot
Most people use camelCasein JS. If you want to open-source anything, I suggest you to use this one :-)
大多数人camelCase在 JS 中使用。如果你想开源任何东西,我建议你使用这个 :-)
回答by arunram
According to me: For files, use lower camel case if module.exports is an object, I mean a singleton module. This is also applicable to JSON files as they are also in a way single ton. Use upper camel case if module.exports returns a constructor function where it acts like a class.
据我说:对于文件,如果 module.exports 是一个对象,则使用小驼峰式,我的意思是单例模块。这也适用于 JSON 文件,因为它们在某种程度上也是单吨的。如果 module.exports 返回一个构造函数,它的作用就像一个类,则使用驼峰大写。
For folders use short names. If there is need to have multiple words, let it be completely lower case separated by "-" so that it works across all platforms consistently.
对于文件夹使用短名称。如果需要有多个单词,就让它完全小写,用“-”分隔,这样它就可以在所有平台上一致地工作。
回答by Subhas
Node.js doesn't enforce any file naming conventions (except index.js). And the Javascript language in general doesn't either. You can find dozens of threads here which suggest camelCase, hyphens and underscores, any of which work perfectly well. So its up to you. Choose one and stick with it.
Node.js 不强制执行任何文件命名约定(除了index.js)。Javascript 语言一般也没有。您可以在此处找到许多建议使用驼峰式大小写、连字符和下划线的线程,其中任何一个都可以很好地工作。所以这取决于你。选择一个并坚持下去。

