P
P
pmcode2017-01-19 20:19:29
Node.js
pmcode, 2017-01-19 20:19:29

How to organize a modular application on NodeJS / Electron so as not to suffer?

There is an application on Electron. The directory structure looks something like this:

├── app
│   ├── build
│   ├── index.html
│   └── src
│       ├── components
│       ├── entry.js
│       ├── MainPage.vue
├── node_modules
├── persistence
│   ├── testmodule.js
│   └── package.json
├── electron.js
├── package.json
└── webpack.config.js

All logic is placed in externals , because on Electron there is no point in bundling everything into one webpack file (in the example, the logic is the persistence module).
And then the NodeJS / NPM rake begins. To include testmodule.js in MainPage.vue, I can:
1. Use classic NodeJS trash:
require(path.resolve(__dirname, "/../../persistence/testmodule")) // это из MainPage.vue
require(path.resolve(__dirname, "/../../../persistence/testmodule")) // а это из компонента чуть пониже
require(path.resolve(__dirname, "/../../../../persistence/testmodule")) // и еще пониже :)

..and pray that you never have to do any refactoring of the directory structure afterwards.
2. Add persistence as local dependency
"dependencies": {
    "persistence" : "file:./persistence"
}

Then the modules can be connected relatively simply. Everything seemed fine, but for some reason NPM doesn't respect such local dependencies. I named my module persistence and NPM already has one . If you do npm update, then it will pull the module from its repository. You can, of course, use scope, but where is the guarantee that someone will not like it either.
And secondly, npm update doesn't have a force option. To update a module, you need to delete the directory and do npm install, which is not very convenient.
3. And there is also npm link, but it is convenient for fumbling with modules in m / y projects, but this is not required at all.
Please tell me the correct way. Well, if at all possible.
UPDATE:
Basically, I found it. Here is a thread where the developers are ignoring this use case. And herecrutch (quite working). Everything is as always in the world of JS, in general. :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Shilka, 2017-01-26
@glitch536

About 2
I use the NODE_PATH environment variable to launch the node and the path for require (for a file of any nesting) I prescribe from the project directory. Something like this
package.json:

"scripts": {
  "dev": "NODE_PATH=. nodemon app/index.js",
},

app/lib/something/index.js file:
well, you also need to set this variable on prod

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question