How to make a Nodejs distributable command line application - node.js

I'm making a command line application with commander, inquirer and nightwatch as top dependencies. The main purpose of the app is for automation and testing.. Is there any way i can make this distributable instead of publishing it as npm package. I want the same functionality as those cli made with python, where it can be setup, and run on a target machine. is this possible? Thank you

Here are two open source projects (PKG and Nexe) that enable you to package your Node.js project into an executable:
https://github.com/zeit/pkg/blob/master/README.md
Or
https://github.com/nexe/nexe/blob/dev/README.md
You can use either one to make an executable of your project.

Related

Is it possible to install and use node modules in my Chrome Extension? [duplicate]

I tried so but I have a 'require is not defined' error. I can't find information about that, can someone enlighten the noob in me please?
It's possible, but you have to be careful. Trying to require() a package means that node will try to locate its files in your file system. A chrome extension only has access to the files you declare in the manifest, not your filesystem.
To get around this, use a module bundler like Webpack, which will generate a single javascript file containing all code for all packages included through require(). You will have to generate a separate module for each component of your chrome extension (e.g. one for the background page, one for content scripts, one for the popup) and declare each generated module in your manifest.
To avoid trying to setup your build system to make using require() possible, I suggest starting with a boilerplate project. You can check out my extension to see how I do it.
An updated answer for 2022
Short answer: yes, you can require/import packages. Rather than going through the tedious work of setting up & configuring a bundler like Webpack on your own (especially if you have no experience with them), there are now build tools you can use to create the boilerplate "scaffolding" for a Chrome extension:
Extension CLI -- this one is well-documented and you can also reference the source code of some Chrome extensions that have used this tool (READ: learn how others have set up their code).
Chrome Extension CLI
Benefits of using them:
New projects are initiated with a default project file structure. Super helpful.
They support modern Javascript (ES6, ES2021), so modules work fine.
They already have bundlers integrated and pre-configured (Webpack in both above cases I think). You therefore don't need to install and configure any on your own.
You can use npm as normal to install any packages/dependencies you need.
Then of course, let the official documentation for Chrome Extensions guide you through the rest.
It's not possible to require node modules directly within a chrome extension. However, it is possible to bundle node applications and packages into the browser for use with your extensions. See here for more: Is it possible to develop Google Chrome extensions using node.js?
Yes, It is possible with esm npm packages.
require is commonjs module loader.
Browser doesn't support commonjs modules system
so that this error showed.
Method 1:
Run npm init -y and add "type" :"module" in your package.json.
create path.js file
add this line in path.js
const fullPath = await import.meta.resolve("npm-pkg-name");
const path = fullPath?.match(/(/node_modules.*)/)[0];
console.log(path);
add this line inside package.json
"path": "node --experimental-import-meta-resolve path.js",
Copy console output text. Replace package name with this copied path.
Method 2:
Install other npm package to find and replace
npm packages' virtual path to real path so that chrome browser will find it.
Install Path-fixxer
Add this line in path.js
import setAllPkgPath from "path-fixxer";
setAllPkgPath();
then run command : npm run path.
Now open browser to test it.

Making node.js code to a executable file in MacOS

I have a nodejs app app.js which I'm able to execute with help of the command line tool (changing to the direction of the app and executing the command
node app.js
before that the application is installed with npm install). To make it easier in the daily use i would like to make the app executable, both parts, the installation as well as the trigger of the app.
The app is for generating a small report, I already tried it with a script where I saved the command node app.js.
You might use pkg.
This command line interface enables you to package your Node.js project into an executable that can be run even on devices without Node.js installed.

Can i change a file located in a module under node_modules folder

Is it possible for me to make changes in a file of a module under node_modules directory (by SSHing to that stage unix box) and expect the UI to reflect the changes just be restarting that app on that box? Without having to rebuild?
Yes it would work but I wouldn't recommend it, it would be better to fork the module and publish your own version of it to npm with the npm publish command and use your custom module instead. If it is a bug fix, do the above but issue a pull request while trying to honor the contribution guidelines.
That is unless you are using a bundling tool that minifies and compiles the js modules to one file, in this case you would have to rebuild with the bundling tool you are using such as webpack, a grunt script etc.

Is it possible to run an script when installing a cordova plugin?

I know when I install a cordova plugin the native files are copied into the platform folder and will be compiled when I build that platform. But is it possible to execute an script to for example download additional binary files, or build custom frameworks?
What I am looking for is a way to specify in plugin.xml to execute a particular shell script or install an npm module when the plugin is first installed into a Cordova project. Is this possible?
That is not possible using the 'cordova plugin add' command (not what they were designed to do) but definitely possible if you use task manager tools like Grunt to automate your process.
You can combine tasks to run in order, such as running your own shell scripts before/after you make a call to install plugins.
Check out npm grunt for more info.
Yes, you can
check this:
https://cordova.apache.org/docs/en/latest/guide/appdev/hooks/index.html
You only have to make a folder, called "after_plugin_add", inside the hooks folder of the project, and add there your script.

How to do a dojo build using nodejs?

I'm currently using the dojotoolkit and its build system.
I read the new build tutorial for 1.8 at http://dojotoolkit.org/documentation/tutorials/1.8/build/.
In the tutorial it mentions that you can speed up your build by using nodejs.
The build tool itself relies on Java (and, optionally, Node.js for even faster builds), so make sure that have you that installed as well.
But it fails to mention how to do this. Anyone know how this works?
I normally run it like this:
> node dojo/dojo.js load=build --profile myprofile.profile.js --release
This would build a release for the profile contained in myprofile.profile.js. It assumes you are in a directory, which contains both dojo and util as sub-directories. It also assumes that the path to node is set correctly.
If node is not configured in the path variable, you will need to use the full path to node:
> <path to node here> dojo/dojo.js load=build --profile myprofile.profile.js --release
On windows the path is normally C:\Program Files\nodejs\ but you might have to configure it as C:\PROGRA~1\nodejs\ to get it working.
Windows Notes:
The build scripts do not work with Node on Windows (except using Cygwin). If you are using Windows you can get it to work via the following patch:
Windows Patch
Use the attached node-win.patch file to edit the files: util/build/main.js and util/build/transforms/writeOptimized.js. The patch has worked for me 100% of the time and it a simple matter editing a few lines of code.
I've personally found the alternative to Node, using Rhino, useless. It always fails to detect the build paths correctly, no-matter what I set basePath to. I would strongly advise using Node over Rhino as it is more reliable and easier to setup.
The buildscript util/buildscripts/build.sh checks if node is in your path and if so uses it.
This is currently not working under Windows (http://bugs.dojotoolkit.org/ticket/15413).

Resources