Is there any way to open nw.js app from web browser? - node.js

I have a desktop application, packaged using node-webkit JS. Is there any way to open this app with IP address from other computer by browser? I just set node-remote to http://localhost:3000 in package.json but is not working when I use chrome and open the IP. There are some errors like nw is not defiend and etc. Please tell me if this way can work or not. Thanks

I don't know nwjs but if I understand correctly, you want to access to localhost in your computer from another computer.
you can not use ip to access because of NAT. but fortunaly you can do that by a third computer (that is not behind NAT). to do that use localtunel.
for more info see this.

Do you think you could paste what your package.json looks like? I've done what you are talking about. Here is what my file structure looks like:
!(https://i.imgur.com/L3M6lvx.png)
The package.json that is in my project folder:
!(https://i.imgur.com/uZV7mzr.png)
The 1st thing that I did was install my dependencies into my project folder so that I don't get the command not recognized error. I did that by going to my project folder and typing:
npm init -y
npm install nw#0.50.1-sdk nwjs-builder -D
This creates a fresh package.json and adds the modules to the file as dependencies. Then I went into my src folder and created another package.json. I set the "main" tag to my index.html
Going back to the .json in my root project folder, we add to the "script" tag:
"script": {
"dev": "nw src/ --remote-debugging-port=9222"
}
(you can make dev whatever you want)
Once you have that setup, all you need to do is run npm run dev and your app will open up. Head over to chrome and type localhost:9222 and you should be set.

It is possible to create an app that can run in a regular browser, and also in NW.js with added features when it runs inside NW.js. You would need to basically wrap anything in if statements, like
if (window.nw) {
let fs = window.nw.require('fs');
let file = fs.readFileSync('./whatever.txt');
console.log(String(file));
}
You could then create two different npm scripts. One to just run a local web server and one to run it and launch NW.js.
{
"main": "http://localhost:4467",
"node-remote": "http://localhost:4467",
"node-main": "server.js",
"scripts": {
"start": "concurrently \"npm run serve\" \"wait-on http://localhost:4467 && nw .\"",
"serve": "node server.js"
},
"dependencies": {
"express": "latest"
},
"devDependencies": {
"concurrently": "latest",
"wait-on": "latest"
}
}
Example: https://github.com/nwutils/nw-local-server-example

Related

Node.js applies the changes only after restart

I am very new to server side scripting. And I am using NodeJS. My Problem is that after adding some new features to the app, i.e. after changing the code, these changes will be applied only after restarting the server. Till then NodeJS behaves so as though I hadn't changed anything. So for instance if I add console.log("works") and don't restart the server, then it hasn't any effect.
I am using Nuxt.js, which is actually the Vue.js framework but with additional and very usefull features mainly for server side rendering. I didn't integrate the express.js at the beginning of the project, beacause it wasn't planned to write any server side code. So I am normally exporting express and using it, which is pretty fine for me, since I need just a couple lines of code to use the NodeJS file system.
So, as it is pretty hard to code, if I should restart the server once I changed anything, I want to ask you if there is any solution to this problem.
Use nodemon
step 1 : npm install -g nodemon <- this will install nodemon globaly in your system
step 2 : change your start script within package.json
"scripts": {
"start": "nodemon fileName" <- like this //filename is you root file which starts the app like app.js
}
step 3 : npm start
This is already build in into nuxt. You just need to run it in dev mode, not in production.
E.g. for dev with change monitoring
nuxt
For production without monitoring
nuxt start
So in this particular case the following changes to the "scripts" in package.json have solved my problem.
"scripts": {
"dev": "nodemon --watch api --exec \"nuxt\"",
"start": "nodemon nuxt",
}
The following link could also be usefull to you.
Install nodemmon in your application to allow live update npm -g install nodemon
and add the following codes inside your packages json file :
"main": "app.js",
"scripts": {
"start": "node app"
},
on your command line, just type : start

How to run basic hello world app using electron framework , node.js on windows platform

I have created the package.json, main.js and index.html files, stored them all in the folder, but when I try to run the application the following errors are displayed.
There could be two solutions if you want to use npm start (fixing either them should be enough):
No start script in package.json
No server.js file
Number 1 solution:
Do you have a start script in your package.json file? - as it looks like you may be missing it.
To fix this, go into your project directory, and open up your package.json file, and add the following script (replacing your-file with your actual file name):
"scripts": {
"start": "node your-file.js"
}
Number 2 solution:
Change your application name (e.g. myapp.js) to server.js, and try running npm start again.
OR SIMPLY... just run the node myapp.js (replacing myapp.js with your actual file name that you want to run) command rather than npm start
Replace your package.json with this one
{
"name" : "arun_electron",
"version" : "0.1.0",
"main" : "main.js",
"devDependencies": {
"electron": "^0.4.1"
},
"scripts": {
"start": "electron main.js"
}
}
when you run npm start, the command electron main.js will be executed.

npm start Microsoft JScript runtime error 800A138F object expected

Getting started with NPM...
I can run the following command to start my app.
node app
But when I run the following command, I get the following error:
npm start
Windows Script Host
Object expected
Line 2
800A138F
Microsoft JScript runtime error
App.js:
var port = 5000;
var express = require('express');
Package.json:
{
"name": "2",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "app.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.13.3"
}
}
And I've run this command of course:
npm install express --save
Seems like NPM can't find the express package? Why not?
I know its too late to answer, but in case any wanderer like me faces the same problem in future..
Solution:
need to add node keyword ahead of app.js in script. So, start script should
"scripts": {
"start": "node app.js"
},
That solved for me.
I had this problem as well. The .js files are associated with the Microsoft Script Engine. Some suggested that we dis-associate the extension with the program, but I do not think that is necessary. I was able to fix the issue with the following instruction that I found in the NODE Docs.
Please make sure that your file(s) referenced in bin starts with
#!/usr/bin/env node
otherwise the scripts are started without the node executable!
Use >node app.js instead of >app.js
Then it will work
When I faced the same problem using Visual Studio Code, I was trying to run localhost at port 8080, however by pressing F5 I was able to know that the port was already taken and so I changed the port to 3000, and viola! that worked for me.
Check out the error
If you're using Visual Studio Code, it may have taken over running the script. If in VS Code, hit F5 and see if the project runs properly.
I don't know why it does this, but did for me. This tipped me off

How do you update Openshift NodeJS Cartridge's Express from 3.x to the latest 4.x?

When Openshift creates a Node.js cartridge it includes a version of Express 3. My app is an Express 4 app and fails to start under the default Openshift setup. Even if my app's package.json has the line "express": ">=4.9.0" in dependencies.
4.9.0 happens to be the version that is embedded in my app's project but is ignored by Openshift when started there. So apparently I need to update Openshift's version to 4. I can confirm that the app works as designed and intended on my local computer.
How do I update Openshift's Express, which is outside the app, from version 3 to 4 ?
I made sure that my /package.json includes something like this under dependencies:
"express": "~4.11.1"
Personally, I retired my /bin/www content by removing that out of /package.json:
...
"scripts": {
"start": "node server.js"
},
"main": "server.js"
}
...and migrated much of /bin/www back into /server.js.
That seemed to be the only way I could get Express4 to work on OpenShift.
/server.js needs a shebang a the top "#!/bin/env node"
/package.json gets the mod above
I've got my Express4 app running now with MongoDB support. Seems to be happy. Pushes/builds/logs success.
Which works for me is:
"main": "./bin/www",
"scripts": {
"start": "node ./bin/www"
}

How to deploy a MeteorJS app to Windows Azure? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How does one deploy a production MeteorJS app to Windows Azure?
Yes it is. See http://www.meteorpedia.com/read/Deploying_to_a_PaaS
In most cases this is as simple as using "meteor bundle",
demeteorizer, and then uploading the resulting files with your PaaS
provider's CLI deploy tool.
Demeteorizer wraps and extends Meteor’s bundle command by creating
something that more closely resembles a standard looking Node.js
application, complete with a package.json file for dependency
management.
$ cd /my/meteor/app
$ demeteorizer -o /my/node/app
$ cd /my/node/app
$ npm install
$ export MONGO_URL='mongodb://user:password#host:port/databasename?autoReconnect=true&connectTimeout=60000'
$ export PORT=8080
$ forever start main.js
Forever keeps your app running after a disconnect or crash, but not a reboot unless you manually add a boot entry.
The whole deploy is much easier using Meteor Up instead. Or maybe mups, though that doesn't even have updated docs.
To run a Meteor app in an Azure web app:
Azure Web App
Python 2.7
Websockets ON (optional)
WEBSITE_NODE_DEFAULT_VERSION 0.10.32 (default)
ROOT_URL http://webapp.azurewebsites.net
MONGO_URL mongodb://username:password#instance.mongolab.com:36648/dbname (For advanced apps. Request log should say if you need it.)
Dev Machine
Install Visual Studio Community 2015
Install Node 0.12.6
Install Meteor MSI
app> demeteorizer -o ..\app-dem
app-dem\programs\server\packages\webapp.js change .PORT line to "var localPort = process.env.PORT"
app-dem\package.json change "node": "0.10.36" to "node": "0.12.6"
app-dem> npm install
app-dem> git init
app-dem> git add -A .
app-dem> git commit -m "version 1.0 demeteorized Meteor + tweaks"
app-dem> git remote add azure https://username#webapp-slot.scm.azurewebsites.net:443/webapp.git
app-dem> git config http.postBuffer 52428800
app-dem> git push azure master
Instead of demeteorizer -o, perhaps you could use meteor build and create a package.json in the output root:
{
"name": "App name",
"version": "0.0.1",
"main": "main.js",
"scripts": {
"start": "node main.js"
},
"engines": {
"node": "0.12.6"
}
}
If bcrypt doesn't compile, make sure to use a more recent version:
"dependencies": {
"bcrypt": "https://registry.npmjs.org/bcrypt/-/bcrypt-0.8.4.tgz"
}
Before starting make sure your have install'd a 32 bit version of nodejs and have run "npm -g install fibers" on your windows build machine. Default nodejs on azure is running 32 bit only!
Note: this will not work if you'r using for example the spiderable package which relays on PhantomJS. PhantomJS can not be executed in a webapp on azure?
In your project "meteor build ..\buildOut" and extract the .tar.gz file located in "..\buildOut".
Place/create in "..\buildOut\bundle" a "package.json" containing:
{
"name": "AppName",
"version": "0.0.1",
"main": "main.js",
"scripts": {
"start": "node main.js"
},
"engines": {
"node": "0.12.6"
}
}
Note: Make sure "name" doesn't contain spaces, the deploy on azure will fail.
On your favorite shell, goto "..\buildOut\bundle\programs\server" and run "npm install". This will pre download all the requirements and build them.
Now open the file "..\buildOut\bundle\programs\server\packages\webapp.js" and search for "process.env.PORT".
it looks like this:
var localPort = parseInt(process.env.PORT) || 0;
alter this line into:
var localPort = process.env.PORT || 0;
This is needed so your meteor project can accept a named socket as soon as it runs in node. The function "parseInt" will not let a string go thru, the named socket is a string located in your webapp's environment. This my be done for a reason, a warning here! Now save this change an we are almost done...
Solve the bcrypt issue: Download this file and extract it somewhere: https://registry.npmjs.org/bcrypt/-/bcrypt-0.8.4.tgz
Extract it.
Now replace the files located: "..\buildOut\bundle\programs\server\npm\npm-bcrypt\node_modules\bcrypt*"
with the directory's and file's located somewhere: ".\bcrypt-0.8.4\package*"
Now go on the shell in the directory "..\buildOut\bundle\programs\server\npm\npm-bcrypt\node_modules\bcrypt\" and make sure you remove the "node_modules" directory. If the node_modules directory is not removed npm will not build the package for some reason.
Run on the shell "npm install".
Make sure you set the "Environment" variables: "MONGO_URL" and "ROOT_URL" in the portal for you webapp.
If everything worked without an error, you can deploy your app to the git repository on the deployment slot for your webapp. Go to "..\buildOut\bundle" and commit the files there to the deployment slot's repository. This will course the deploy on the deployment slot and create the needed iis configuration file(s).
Now wait a little and your app should fire after some time... Your app should be running and you can access it on the *.azuresites.net
Thanks to all that made this possible.

Resources