As in my comment, it's difficult for the SO community to dictate how you structure your application without knowing a lot more about what the application does.
However, you have asked if node_modules
would be a good place to execute your application code...
The answer to that is a resounding 'no'. The node_modules
folder is specifically for modules that are used by the application, not the application itself.
A typical node application looks like this:
├── app.js
├── package.json
├── node_modules
├── module1
├── module2
├── etc...
Generally, your application would be written in app.js
, and your external modules will be stored in node_modules
by the Node Package Manager when executing the npm install
command on the command line. NPM installs the packages that are listed in the package.json
file. It would be bad practise to store application code in node_modules
because this folder is often assumed to be re-buildable through NPM, and is usually removed by various applications/developers to make your application more transportable.
If you want to split your code into smaller chunks, then you can require specific files instead of known-modules.
Here's an example:
folder structure
├── app.js
├── lib
├── my-local-module.js
├── package.json
├── node_modules
lib/my-local-module.js
module.exports = function() {
console.log('Hello World!');
}
app.js
var myLocalModule = require('./lib/my-local-module.js');
myLocalModule(); // Hello World!
Every program needs to start somewhere. In this case, you would bootstrap this application by running node app.js
on the command line. That code, in turn, would localise your my-local-module.js
file through the require
statement.
As I said before; my advice would be to keep your code inside one file until you feel your codebase has gotten big enough to warrant refactoring and delegating logic to other files.