How to write a node-webkit application with custom modules? - node.js

I am currently writing a node-webkit application. The application supports certain modules which lie in the module folder inside the application.
I would like to dynamically install and remove those modules from inside the application. For that i would need to dynamically write inside the application.nw file. Is there a best practice solution to my problem?

That's not really a good idea. Just create a folder for app files and put downloaded packages there.

Related

How to figure out which NodeJS core modules a package is using?

Let's say we have a package on NPM like got.
I want to figure out which core NodeJS modules this package is using as I want to polyfill it on environments (like React native, or the browser).
Is there any way where I can just get a list of which core NodeJS modules this package is using without manually searching through the source code?
I know there are ways to figure out the dependency tree -- that is not what I am asking. I specificially want to know which core NodeJS modules are being used.
There is no way to do this that is not manual unfortunately.
However, it is pretty simple to do.
Simply search all the files in your project via your IDE. CMD+F for things like crypto, buffer, stream, etc.
https://flaviocopes.com/node-core-modules/ is a list of all the core node modules

How to structure your NodeJS application in different modules?

so far i've learned a bit about NodeJS. But now i want to write a huge enterprise app with it and i'm wondering how to setup the structure correctly? Coming from other languages like PHP and Java, i imagine, i would split my project in different NPM modules. For example #mybigproject/customer, #mybigproject/cart and #mybigproject/checkout and so on.
But those submodules would be installed in the node_modules folder of the application skeleton. How would i tell for example Express, that the template files are in the different module directories? Or for example i use TypeORM for data access. So each module would have it's own set of models. How do those models know the database configuration data, as it's only in the main application skeleton, or the other way around, how does the application skeleton should know where to find the models?
Don't use npm modules for different parts of your project.
This components is integral part of your project and usually depend on your global config / schema / routing / etc
Just put it in different files and require it where you need it.
You can get an idea for folders structure from projects like Sail.JS
Use npm modules if you writing some utility that going to serve you for different apps and you want an easy way to upgrade the utility code once for all your apps (or in case you want to share that utility as open source for all of us)
NPM can install your local folder as a dependency. (ref)
npm install <folder>:
Install the package in the directory as a symlink in the current
project. Its dependencies will be installed before it's linked. If
sits inside the root of your project, its dependencies may be
hoisted to the toplevel node_modules as they would for other types of
dependencies.
Your module keeps its original location after installed and a symlink is created as the same name of your module folder in the top level node_modules folder.
In these custom sub-modules, you can use __dirname and relative paths to locate you configuration files to feed to database or other data consumers.
But remember that, sub-modules often serve as utility functions for the main module. They should be independent from the project context.

Use NodeJS Helper Script in Meteor

I have written a small helper module in regular NodeJS to be used with NodeJS batch scripts. I've placed this and all the batch scripts in the "private" folder inside my Meteor project.
I'd like to also use the helper module on the server-side of Meteor as well, but I don't know the best way to handle that.
This is my current project structure:
client
... client files ...
private
scripts
helpers.js
batch_script1.js
server
... server files ...
So for Meteor to include the "helpers.js" file into the server, it either has to be located in the "server" folder, or imported via a package. Creating a symlink won't work, as multiple developers will be working on this and may have the repository checked out to a different directory location (seeing as how you need an absolute path to create a symlink).
I also don't want to have to duplicate the file and maintain two copies, so what are my options for sharing a helper script between a Meteor app and a NodeJS script?
Thanks
I was able to find help on the Meteor forums: https://forums.meteor.com/t/use-nodejs-helper-script-in-meteor/11056/3

Deploy a node.js application (windows)

for a customer of mine i'm developing a node.js app.
I want to give him a simple setup program that handle the installation of node and relative modules in automatic without having to access the web.
Which is the best and efficient way to do that?
Thanks in advance
Just give him the node.js installer und zip your app (with node_modules directory). He just has to install node.js and extract the zip file. You could write a .bat file which starts the app. That should be easy enough.

packaging node.js modules with more than code

I'm putting together a module I'd like to release, but am a bit stuck on how best to go about packaging it up. In addition to server side javascript, the module will need things like an admin screen, and client side javascript files. That is, it needs to serve out a fixed set of static html/css/js files. (I may have the node-static module as a dependency)
I'm curious what is the best way to handle this. I'd like to make this simple to install and integrate into apps, without forcing the user to dig through a long README. Basically they should be able to NPM the module, then add a line or two of code in the relevant place, and have it "just work". I don't want them to have to download other stuff, tell the module where to find the static files, etc.
Also, I'd like to make sure it can be included in both simple apps (i.e. one step from the standard "hello world") as well as complex apps using frameworks etc like Express, without undue hassle.
Is this possible, or is this beyond the scope of what the module system is designed to handle?
Once your package in installed with npm install mypackage -g you can use __dirname inside your executable to find the directory it's running in.
Likely /usr/local/lib/node_modules/mypackage/bin/mypackage
With your assets in /usr/local/lib/node_modules/mypackage/assets
so __dirname + '../assets' + myasset should correctly find your asset

Resources