Error when deploying a node.js app on a PLESK ONYX server with Phusion Passenger V. 5.3.5 - node.js

I want to deploy a small node.js app on a PLESK-ONYX server - on which Phusion_Passenger / 5.3.5 is also running!
The following happens:
This works here:
var express = require ('express');
var bodyParser = require ('body-parser');
That does not work:
import express from 'express';
import bodyParser from 'body-parser';
The following error then occurs:
The Phusion Passenger application server tried to start the web application through a Passenger-internal helper tool called the "wrapper". But Passenger was unable to execute that helper tool because it encountered an internal error.
The stdout/stderr output of the subprocess so far is:
/var/www/vhosts/hosting13.netcup.net/mem3-s3.hxxx-3.de/index.js:2
import express from 'express';
^^^^^^^
How can one solve it?
Where is the problem?
Thank you for your help in advance!
Best regards
Markus
Hello, I have new findings in the meantime ...
my problem seems to be - that the highest-available node.js version in PLESK at the moment is 12.4!
In PLESK - i must start the node.js-App with a server.js-File - like this:
const app = require('./index.mjs');
const http = require('http');
http.createServer(app).listen(process.env.PORT);
My node.js-Version is V.12.4 so i must use esm like this:
require = require("esm")(module/*, options*/)
module.exports = require("./index.js")
How can i combine the 2 files in the right way?
Thanks a lot for your help!
Here is the content of "package.json":
{
"name": "mb-test-1",
"version": "1.0.0",
"description": "",
"main": "index.mjs",
"type": "module",
"scripts": {
"start": "nodemon -r esm index.mjs"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv":"^8.2.0",
"esm":"^3.2.25",
"express":"^4.17.1",
"mongoose":"^5.9.29",
"nodemon":"^2.0.7"
}
}
But unfortunately I haven't found yet - where in PLESK the start command for node.js can be edited!
Best regards
Markus

for using import from you should change type in package.json to module so add this code to the package.json
"type": "module"
Note: but export file is different between commonjs and module.
by default type is commonjs , check this article
you can also using babel module, update scripts in package.json like this
"scripts": {
"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
"start": "npm run build && node dist/app.js"
}
after that for running project
npm start

Related

How do I fix an Internal Server Error?- Google Cloud

I am following this tutorial on Google Cloud Platform and am finding that the SIMPLE app launches successfully up on port 8080 but when I head over to the browser to view it externally, I get an internal server error. The tutorial can be found here:
https://cloud.google.com/appengine/docs/standard/nodejs/building-app/deploying-web-service
I am using Win8.1 using the GCP cloud shell terminal.
I have also tried updating my npm packages, moving my yaml, nodejs files to the next higher directory as well as deleting the package.json in the next higher directory. Like I said, port 8080 can come up and logged to the terminal, but does not come up in the browser. My package.json is as follows:
{
"name": "express",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": ""
}
My app.yaml file is
runtime: nodejs10
And my server.js file is
//this is a test by MP2
// date of use : 2020-0601
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from App Engine!');
});
// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});
Any help would be great since I am new to the google cloud. What is also interesting is that the most current version of express is installed at the next higher folder but not in the current one where the desired JSON is. ( and unable to update it) Thank you.
-MP
It seems that this is because there is no express dependency in package.json:
"dependencies": {
"express": "^4.17.1"
}
I have replicated all steps and it worked. In the tutorial there is 4.16.3 version, I have different, because I have used npm install express -s and it added version automatically to package.json. However it works with 4.16.3 as well on my side.
I hope it will help!

Error using Postgres with a newly created React app: Module not found: Can't resolve 'dns'

I am probably missing something very obvious here, but I keep getting the following error when trying to use Postgres from a newly created React app
Module not found: Can't resolve 'dns' in '/Users/tarek/test/node_modules/pg/lib'
Here are the steps to reproduce:
Create a fresh React app using create-react-app
Add the Postgres
module using npm install pg --save
Add the following line to
App.js: const { Client } = require('pg');
Type npm run start and you'll get the above error.
What am I missing here?
**Node version: v8.12.0
** package.json
{
"name": "test",
"version": "0.1.0",
"private": true,
"dependencies": {
"pg": "^7.4.3",
"react": "^16.5.1",
"react-dom": "^16.5.1",
"react-scripts": "1.1.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
The issue is that React apps are meant to run in the browser, and browsers can't access databases directly. Even if you did find a way to do this, it would only work locally and not in production. Consider that if the database is on your machine and your app available on the internet, there would be no way for a remote user to get access to the data on your machine in the way you are describing.
In order to use a database with React, you'll need to set up a server—possibly written in Node (with Express)—that will serve your application and data for it.
The server receives requests from different users and responds with the appropriate resources. For example if they go to your website (e.g. example.com), your server should respond with index.html for your project (in this case it would be the index.html for your React app). Then to get data from your database, you can set up an api with routes like example.com/api/model/1 so that your react app can request the instance of model with id 1 from your server, which will grab it from the database and send it over to the React app.
Look into the MERN (Mongo Express React Node) stack for more detail on how all these work together. Even with Postgres the process will be pretty much the same as with Mongo.

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"
}

Working Node.js modules into an application directory structure

I've begun using Node.js to make web applications. It's really awesome. I've come across a few modules that I want to incorporate into my build. I can work with the modules in Terminal after a global npm install. When it comes time to add them to my application, I have no idea how to go about placing them in my directory structure and I haven't found any good documentation on this. My typical node.js directory is:
ROOT
Server
server.js
node-modules
Client
index.html
css
-main.css
javascript
-main.js
-jquery.js
My process for installing the modules has been:
I cd into my Server file and run npm install
Then I go to my package.json file and include the module in the dependencies
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "3.1.0",
"jade": "*",
"stylus": "*",
"<node-module-here>": "1.0.x",
},
"engines": {
"node": "0.10.0",
"npm": "1.2.14"
},
}
After that, I head over to the server.js file I add:
module.exports = require('<path_to_node-module_lib>');
When I run functions that are dependent on the modules on the Client side (functions that work in Terminal), I don't receive an error but the function won't run. Because I'm not receiving errors I have no idea about how to debug. If anyone can recognize some fatal flaw in my structure or implementation and can offer some recommendations, I offer my first born.

Resources