Running Next.js App on EC2 Instance with IIS - node.js

I have an AWS EC2 instance for my Next.js website. If I open the terminal on my Next.js app and run next start it runs fine and I can visit the website. However, I then need to always keep the terminal open on the instance for the website to work.
My question is, can I use IIS to keep a Next.js website running with next start? I don't want to create a full node backend to serve static html pages because it will mess up some of the dynamic aspects of the website. So is there a way to run a Next.js app by itself without running next export for the static pages?
UPDATE
I attempted to run the Next.js app with PM2 instead. When I run npm start in the terminal it works fine. However, when I run pm2 start npm -- start (or any variation of that), it stops the server and logs this error multiple times:
SyntaxError: Unexpected token ':'
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Object.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:33:23)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
:: Created by npm, please don't edit manually.

You can use forever to run your process in the backend, the below example is how to implement it with iis
https://www.technoapple.com/blog/post/run-node-as-background-by-using-forever

Related

Deployement Nuxt.js ssr on passenger : SyntaxError: Cannot use import statement outside a module

I'm deploying my nuxt app on planethoster who use Passenger to run Node.js Application. I build the app by using ">npm run build" and deployed the content off .nuxt folder on the server, with "server.js" as entrypoint. but I ran on the following error:
/home/scalcmjx/test.scalomhouseplans.com/server.js:1
import Vue from 'vue'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at Module.require (/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/node-loader.js:80:25)
at require (internal/modules/cjs/helpers.js:74:18)
at loadApplication (/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/node-loader.js:243:2)
at setupEnvironment (/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/node-loader.js:214:2)
Please Someone know how to manage it?
What is your node.js version? Try using the current LTS: 14.17.0
If I'm not mistaken, the import syntax is officially supported since v14, so this should maybe fix one issue.
Also, you need to deploy dist and not .nuxt, the latter is used for caching purposes.
I'm not sure of what is the process for this specific host, but you should probably get some decent ideas from the deployment section of the official documentation: https://nuxtjs.org/docs/2.x/deployment/deploying-to-21yunbox
There are a lot of hosting solutions there, one is probably looking like Planethoster.

Use of const in strict mode : Azure Web App

Application has thrown an uncaught exception and is terminated: SyntaxError: Use of const in strict mode.
at Module._compile (module.js:434:25)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at Object.<anonymous> (D:\home\site\wwwroot\node_modules\mongoose\index.js:7:18)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
Error is thrown everytime Azure Web App project is made and an Express App is published. I tried with Azure Express as well as Blank nodeJs App to do it
Looks like it's caused by an invalid node version on Azure. Go to Azure portal, your Web App - Application settings, check WEBSITE_NODE_DEFAULT_VERSION.
Once we specify a version not available on Azure, a quite old version 0.10.40 is in use, where const is not enabled by default so that we met SyntaxError: Use of const in strict mode. See related thread for more details.
We can use 10.6.0, 8.11.1, etc. Go to https://<yourwebappname>.scm.azurewebsites.net/api/diagnostics/runtime to see all versions available.
Caveat by Clinkz
In some cases, the above solution may not work. This may be because your project includes the file iisnode.yml. If this file exists, it overrides application settings environment variable. The content of this file should be as follows :
nodeProcessCommandLine: "%SystemDrive%\Program Files (x86)\nodejs\0.10.4\node.exe"
The node version specified here takes precendence. To fix it, simply update the version, like so and deploy:
nodeProcessCommandLine: "%SystemDrive%\Program Files (x86)\nodejs\8.9.4\node.exe"
Refer to this.
To conclude, the priority: iisnode.yml > package.json(engine)> Application settings. Application setting is recommended as it's easy to check and modify on portal.

Socket.io Syntax not Recognized on Server

Node server not able to understand socket.io syntax, even with all packages installed.
When I run my server locally with nodejs server.js it works fine is working. But, when I try to run it on my Ubuntu server, it does not seem to understand this line:
socket.on( 'client-data', ( serverpackage ) => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
The directory on my Ubuntu server is user/server, which contains all my server modules. Socket.io is installed, and I even checked to make sure all dependencies for socket.io are there too.
NodeJS-Socket-server-with-DB#1.0.0 /home/<user>/server
└── socket.io#2.1.1
I am also running this version of nodejs:
<user>#host*****:~/server$ nodejs -v
v0.10.25
You can't use arrow functions. You need at minimum version 4 of node however version 6 offers full compatability with arrow functions.
To fix your issue simply update node or change to a regular function like:
socket.on( 'client-data', function (serverpackage) {
Your nodejs version don't understand arrow function, check update if you want use it.
If not, please change arrow function to normal function.
Check Node.js ES2015 Support.

Expressjs change context where the server executes the app from

I'd like to know if it's possible to change the context from where the Express server is serving from, to avoid issues in the import/require files in the App that is served.
I'm serving from [root]/server.js, this file checks for the process.env.NODE_ENV and then requires a file in let's say ./dist/ENVIRONMENT/server.js
if (['staging', 'production'].indexOf(process.env.NODE_ENV) > -1) {
require('./dist/' + process.env.NODE_ENV + '/server.js')
} else {
require('babel-register')
require('./server.dev.js')
}
The files that are served are triggering an error related with the import/require locations that fails for the reason explained in the previous paragraph
Error: Cannot find module '../../../../xxxxx'
Why am I doing this? I'm transpiling the server source code for non development (that runs with babel) and then copied to the distribution directory. I understand that I can have tasks to rename and move this accordingly during deployment, that also solves the problem, but I'd like to learn or understand if we're able to change the context where the Express server, serves from!
From the top of my head, I guess I'll have to solve it through something like (which I'll be testing):
var dist = path.resolve(__dirname, '../../')
app.use(express.static(dist))
Which I attempted, but failed, same error:
Error: Cannot find module '../../../../config'
at Function.Module._resolveFilename (module.js:485:15)
at Function.Module._load (module.js:437:25)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/xxxx/www/projectFoobar/dist/staging/lib/services/foobarService/index.js:13:15)
at Module._compile (module.js:569:30)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
The config file happens to be in the [root] directory.
** OK just realised that using import/export is a bad practice
https://nodesource.com/blog/es-modules-and-node-js-hard-choices/

Azure/Node.js Unaught exception: Error: Cannot find module 'ms'

I’ve deployed a small node.js app that runs fine locally in visual studio 2015 to Azure using the Azure publish option (right click – publish – azure profile etc.).
However, when I visit the site all I get on the page is:
The page cannot be displayed because an internal server error has
occurred.
If I connect VS server explorer up to the Azure instance to view the logs, inside logging-errors.txt I can see the following logged lots of times:
Wed Jul 29 2015 12:22:36 GMT+0000 (Coordinated Universal Time): Unaught exception: Error: Cannot find module 'ms'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (D:\home\site\wwwroot\node_modules\debug\debug.js:14:20)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
This is a brand new web app provisioned via the preview portal.
There is no ‘ms’ package defined either in my project dependencies or in the node_modules folder, so I'm assuming this is a Microsoft/Azure specific package required for hosting on Azure.
Have I missed a step in “preparing” my node app for Azure, or is there some other configuarion step I need to perform?
Thanks
It looks like it's having trouble loading modules. Do you have a package.json file that defines your dependencies? The default deployment script that Azure runs when you deploy your site will look for a package.json and perform an npm install --production. That means it will install everything you've defined as in 'dependencies' but not the packages you've defined as 'devDependencies'.
To debug node apps deployed to Azure that won't startup I do:
Open a Powershell on it's scm (https://.scm.azurewebsites.net/DebugConsole/?shell=powershell)
Check your node is set correctly node --version
Attempt to start it and look at errors node .\server.js (or whatever is specified in your package.json > start script.
It would be nice if you could see such a log for when Azure starts the app. However I have not found it.

Resources