63

In node.js, it seems I run into the same 3 filenames to describe the main entry point to an app:

  • When using the express-generator package, an app.js file is created as the main entry point for the resulting app.
  • When creating a new package.json file via npm init, one is prompted for the main entry point file. The default is given as index.js.
  • In some programs I have seen, server.js serves as the main entry point as well.

Other times, still, it seems as though there are subtle differences in their usage. For example, this node app directory structure uses index.js and server.js in different contexts:

app
  |- modules
  |    |- moduleA
  |    |    |- controllers
  |    |    |    |- controllerA.js
  |    |    |    +- controllerB.js
  |    |    |- services
  |    |    |    +- someService.js
  |    |    +- index.js <--------------
  |    +- index.js <-------------------
  |- middleware.js
  +- index.js <------------------------
config
  +- index.js <------------------------
web
  |- css
  |- js
server.js <----------------------------

What are the differences, if any, between these three names?

youngrrrr
  • 3,044
  • 3
  • 25
  • 42
  • 9
    They're _just_ names. Don't think too hard about it, just be consistent in your own work. – Oka Mar 15 '16 at 03:58
  • 7
    One thing to note about index.js is that by `require('./config');` you require *./config/index.js* file so it's basicly main entry file for directory. Otherwise they're just names as pointed out by Oka – Molda Mar 15 '16 at 06:09
  • 1
    when running `npm init` it seems that the default is `index.js` – basickarl Sep 22 '16 at 11:56
  • 6
    Someone please provide an answer on why people use app.js instead of index.js – Andrew Lam Dec 06 '17 at 07:39

4 Answers4

44

Even though you can call the files anything you want, there's an advantage to calling the entry point index.js or server.js

Why index.js: When you issue npm init it will set the main entry point of the module to index.js. Some people don't change it, so they end up naming their main entry point index.js. It means there's one less thing to do.

Why server.js: If your node package is not going to be consumed by another package, but rather is a stand-alone app, then if you call your main entry point server.js, then you can issue npm start and start your app. npm start will run your server.js file by default. To change this behavior, supply a start script in package.json. If a start script exists, npm start will run that script instead.

app.js is just a convention -- the only advantage to it is that some IDEs, such as Visual Studio Code will default to app.js as the entry point of a program you debug. That way when using the most common framework, Express, which creates an app.js file, "it just works"

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
17

It's pretty simple!

If your application is to be used in other applications: index.js

If your application is NOT to be used in other applications: server.js or app.js

As stated earlier the reasons being, when invoking npm start, if not defined in package.json, looks automatically for server.js. And when including another module into your application, it looks for index.js.

Extra: I also tend to only use index.js as a file name when this is automatically found somehow. This makes me aware if the file is invoked directly or indirectly.

When using npm init it makes the default index.js.

basickarl
  • 37,187
  • 64
  • 214
  • 335
6

Where I work, we didn't really settle on a format, so we have some apps with index.js, some with server.js. Also, in some we have a config.js file at the root level, others are in a config folder (so require(config/config.js). We even have one where server.js is in a server folder.

The trouble comes when we want to automate our deployment process. It becomes like technical debt when we have to make a bunch of minor modifications for each service.

That said, pick a format that makes sense to you and stick with it.

tjgragg
  • 693
  • 1
  • 7
  • 8
4

In fact all are just names and you must be consistent in your own work as pointed out by @Oka in a previous answer.

The only valid point here is that modular nature of node may play an important role in your decision, as pointed out in Folders as Modules section of NodeJS documentation there are 3 ways in which a folder may be passed to require() as an argument and the second and a common one is to automatically load the index.js file from the folder, this is how a lot of NPM packages are built and as it is simple and standard according to automatically loading NodeJS feature. It seems the best choice if you are developing an NPM package.

In the end, as others pointed out, you can choose any of the three, or even another one, but stick to your decision. My decision was to always use index.js based on the mentioned fact above.

Felipe Plets
  • 7,230
  • 3
  • 36
  • 60
  • 2
    why some people use app.js instead of index.js? there must be a reason right? – Andrew Lam Dec 06 '17 at 07:38
  • 2
    index.php or index.html or index.* dates back from the time when Apache used to rule. Whenever someone requests a directory, it showed a index of files in that directory. So index.html simply replaced that default indexing functionality. So I guess index.js is taken from that convention. But it feels dated, in sense that we no longer "index" directories in node. app.js sounds better and logical to me. –  Jun 20 '18 at 21:41