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

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.

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.

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

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.

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.

Related

How to put .sequelizerc configuration in package.json?

I rather not have multiple configuration files. So I'm trying to consolidate as much as possible.
It's not being documented (at least not well documented) but looking into the source code of Sequelize-cli you can find an options-path parameter:
In your case use it like --options-path package.json
Source code:
cli/src/core/yargs.js

Jest changed test directory resulting beforeAll is not defined

I used to put all my tests together inside of __test__ directory. But I decided to put each test files into each component's directory with different name convention.
Old Structure:
src/
__test__/
example.test.js
example2.test.js
New Structure:
src/
components/
example/
example-controller.js
example-model.js
example-route.js
example-test.js
As naming and location of a directory changed, I updated all import statement and namings for according files. I was using default feature of Jest but since I changed test file name to example-test from example.test I also updated package.json
"jest": {
"testRegex": "./src/components/*/.*.-test.js$"
},
Problem is when I run the project, npm run it throws
beforeAll((0, _asyncToGenerator3.default)( /#PURE/_regenerator2.default.mark(function _callee() {
ReferenceError: beforeAll is not defined
any idea why it is happening?
Try regextester with your file-paths before getting frustrated. Also, look at some popular existing javascript projects using jest and check their jest configurations to see how the community is settling on project structure. It may seem simple to follow others, but most software engineers don't work alone, and usually agree on good practices.
Consider coming back to this project in a year; You are changing the default behavior of a simple process that works beautifully out of the box. Someone may hate you later after a git blame.
If all you want to do is check all -test.js files, slowly modify the default config until you match the file paths you want.
(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$
Should probably (for you) be:
(.*/src/.*(-test).jsx?)

How to use webpack components

Due to the lack of support of modules or packages under development in npm I have decided to use webpack's components which from the look of the example provided here webpack/examples/components seems to be exactly what I am looking for. However there is no example how to actually use the example. Drawing from webpack's convention I thought that:
webpack/examples/components> webpack component.json > bundle.js
would do the trick but nope I get an error. Tried some other stuff like putting an entry file and an output file in the webpack.config.js but no luck there either. Has someone ever use it, does it work and most importantly how to start it?

Define small named packages in your node application without having to publish them to npm

I'm wondering if there is a way with node to make a directory a containing package, like __init__.py does it in python?
If your package is in a directory named "node_modules", and that directory is locatable somewhere shallower in the filesystem tree that the requiring module, require will find it. So you could do:
myapp.js
node_modules/mylib/package.json
node_modules/mylib/index.js
and myapp.js could just require("mylib"). This also works if that node_modules directory is anywhere shallower in the fs.
That's the simple solution but it isn't commonly done because most people want to .gitignore their node_modules directory and only put transient third party stuff in there.
This problem is extensively and exhaustively discussed in the gist better local require paths. I recommend reading through that for other approaches. Ultimately I think things end up as totally separate projects packaged as separate modules, so move toward that end state as soon as it is justified.

require() in large, multi-file Node projects

I'm working on a large Node project. Naturally, I want to break this into multiple source files. There are many modules from the standard lib that I use in a majority of my source files, and there are also quite a few of my own files that I want to use almost everywhere.
I've been making this work by including a huge require block at the beginning of each source file, but this feels awfully redundant. Is there a better way to do this? Or is this an intended consequence of Node's admirable module system?
You can use a container module to load a series of modules. For example, given the following project structure:
lib/
index.js
module1.js
module2.js
main.js
You can have index.js import the other modules in the library.
# index.js
module.exports.module1 = require('./module1');
module.exports.module2 = require('./module2');
Then main.js need only import a single module:
# main.js
var lib = require('./lib');
lib.module1.doSomething();
lib.module2.doSomethingElse();
This technique can be expanded, reducing redundant imports.
I'd say generally that a require block is better practice than using global in Node.
You need to remember that requires are cached so when you put them in all of your code modules, you will always get the same instance not a new one each time.
Doing it this way ensures that you get the appropriate code with the expected name spaces exactly where you want it whereas using global will include things you don't need. Doing it the Node way with require will also tend to make your code slightly more portable.
Well, a couple of things, here.
First, if so many of your files are requiring the same libraries over and over again, you might want to step back and determine if you're really breaking your code up in the proper way. Perhaps there's a better organization where certain libraries are only needed by subsets of your source files?
Second, remember that the global object is shared between all of your required files in your Node.js app. Your "root" source file, say index.js, can do things like global.fs = require('fs'); and then it's accessible from all of your various files. This would eliminate the need to require a file full of requires. (In Node.js, you have to explicitly state that you're accessing a global variable by prepending global., unlike in the browser.)
This can be a good idea for CRUD-type Express apps where you have lots of code for controllers that are all almost the same but have to be slightly different for each view and you just want to split them apart not for any particular organization structure, but just to make it easier to debug (error in this file, not that file). If the structure of the app is more complex than that, take the usual warnings against global variables to heart before using that trick.
Require more than one file without absolute path through require-file-directory.
1- Can require more than one file in single statement.
2- Can require files with only their name.
Visit for solution: https://www.npmjs.com/package/require-file-directory

Resources