Node.js file naming conventions for files and folders

What are the naming conventions for files and folders in a large Node.js project?

Should I use capital, camelCase or sub-account?

Those. Is this considered valid?

project-name app controllers someThings.js users.js models someThing.js user.js views some-things index.jade users logIn.jade signUp.jade ... 
+85
javascript
Sep 20 '13 at 23:30
source share
7 answers

After a few years with node, I can say that there are no conventions for the directory / file structure. However, most (professional) express applications use customization, for example:

 / /bin - scripts, helpers, binaries /lib - your application /config - your configuration /public - your public files /test - your tests 

In an example that uses this setting, nodejs-starter .

I personally changed this setting 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, this is more in line with the unix-style directory structure (while above it shifts a bit).

I also like this file sharing template:

Library /index.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; 

<strong> lib / static / index.js

 var express = require('express'); module.exports = function(app) { app.use(express.static(app.settings.static.path)); }; 

This allows you to completely disable the entire source code without worrying about dependencies. A real good solution to deal with unpleasant javascript. An example of a nearby real world that uses this setting.

Update (file names):

With regard to file names, the most common are short names in lower case. If your file can be described in just two words, most JavaScript projects use an underscore as a separator.

Update (variables):

As for variables, the same "rules" apply to file names. Prototypes or classes, however, must use a camel case.

Update (style):

+128
Jan 02 '14 at 15:08
source share

Use kebab-case for all package names, folders and files.

Why?

You must imagine that any folder or file can be extracted into its own package once. Packages cannot contain capital letters.

New packages should not have capital letters in the name. https://docs.npmjs.com/files/package.json#name

Therefore, camelCase should never be used. This leaves snake_case and kebab-case .

kebab-case by far the most common agreement today. The only use of underscores for internal node packages, and it's just a convention from the first days.

+60
Jul 03 '17 at 9:41 on
source share

There are no agreements. There is some logical structure.

The only thing I can say: Never use camelCase file and directory names. What for? It works, but on Mac and Windows there is no difference between someAction and some actions. I met this problem, and more than once. I need a file like this:

 var isHidden = require('./lib/isHidden'); 

But unfortunately, I created a file with a full lower case: lib/ishidden.js . This worked for me on a Mac. It worked great on my colleague's Mac. Tests run without errors. After deployment, we got a huge error:

 Error: Cannot find module './lib/isHidden' 

Oh yeah. This is a box for Linux. Therefore, the camelCase directory structure can be dangerous. This is enough for a colleague who is developing Windows or Mac.

So use a delimiter (_) or a dash (-) if you need to.

+55
Jan 04 '14 at 22:25
source share

Most people use camelCase in JS. If you want to open source, I suggest you use it :-)

+2
Jan 01 '14 at 18:36
source share

Node.js does not apply any file naming conventions (other than index.js ). And Javascript is generally neither one nor the other. Here you can find dozens of threads that camelCase offer, hyphens and underscores, any of which works just fine. So it is up to you. Choose one and stick to it.

0
Dec 31 '14 at 21:42
source share

For me: for files use the bottom camel case, if module.exports is an object, I mean a singleton module. This also applies to JSON files, as they are also one ton. Use the camel's top case if module.exports returns a constructor function where it acts as a class.

Folders use short names. If you need to have a few words, let it be completely lowercase, separated by a "-", so that it works on all platforms in sequence.

0
Apr 02 '17 at 10:26 on
source share

I already gave +1 to @bodokaiser . The following is just my comment.

The editors.

There are classes like EventEmitter . they followed the Java class of naming classes. Here I follow one point. all globals are a small case (not even camelcase), and all packaged classes are java style - uppercase. which means that if you define your own class, for example function MyFunction(){...} , you usually want to smooth it out.

-one
Jan 03 '14 at 4:01
source share



All Articles