user636044
user636044

Reputation:

What naming conventions exist for the primary Node.js file?

This question has been completely edited in hopes that it will be reopened.

The naming of the main Node.js file is something left to the user and and does not seem to be defined by any well established convention. In hopes of finding a good name, I am curious if there are naming conventions in other parts of the Node.js ecosystem that might suggest a name to use.

Some names I have seen are: app.js, index.js, main.js, server.js, etc.

Please provide only well documented standards in answers.

Upvotes: 2

Views: 2901

Answers (4)

gprasant
gprasant

Reputation: 16023

The two predominant filenames are 'app.js' & 'server.js'. Its better to go with 'server.js'. This is for nodejs applications. In the case of libraries, most libraries use 'index.js' and specify it in their 'main' param in the package.json file.

Upvotes: 1

user636044
user636044

Reputation:

NPM seems to suggest a standard whereby one can define the primary file in the package.json file like so:

"scripts": {"start": "node server.js"}

If no such property is set, NPM looks for a server.js file in the root of the package. If server.js exists, it will be run with Node.

This default seems to be a strong suggestion that the name server.js should be the standard.

Upvotes: 3

dthree
dthree

Reputation: 20730

From what I have seen, app.js is the most universally accepted.

I personally prefer server.js, but this is probably biased in that I run a massive Single Page Application so all my files are .... javascript ..... and I have an app.js controller for my front-end. So it helps me distinguish the two.

do you know how well-established conventions start? you make a decision that is logical, and forthrightly forward it with resolute certainty - then others will follow. there clearly is no well established convention, so pick one and tell it to others.

Upvotes: 0

Bulkan
Bulkan

Reputation: 2596

index.js has a special usage in Node.js. From the Module docs.

...

If there is no package.json file present in the directory, then node will attempt to load an index.js or index.node file out of that directory. For example, if there was no package.json file in the above example, then require('./some-library') would attempt to load:

./some-library/index.js

./some-library/index.node

I prefer to use app.js or even main.js

Upvotes: 2

Related Questions