use sockets with ionic2 - node.js

I need to be able to use udp or tcp sockets on an ionic2 app. I tried using the 'dgram' module that comes with node.js, I can import it but as soon as I try to use it in my class and do ionic serve I see these errors:
Example:
This line alone in my class is fine after "ionic serve"
import dgram from 'dgram';
but as soon as I try to use use, like:
let server = dgram.createSocket('udp4');
and run "ionic serve" I see these errors on the browser:
Cannot find module "dgram
Error: Cannot find module "dgram"
at webpackMissingModule (http://localhost:8100/build/main.js:60926:79)
at Object.<anonymous> (http://localhost:8100/build/main.js:60926:161)
at __webpack_require__ (http://localhost:8100/build/main.js:20:30)
at Object.<anonymous> (http://localhost:8100/build/main.js:103698:14)
at __webpack_require__ (http://localhost:8100/build/main.js:20:30)
at Object.<anonymous> (http://localhost:8100/build/main.js:92433:23)
at __webpack_require__ (http://localhost:8100/build/main.js:20:30)
at Object.<anonymous> (http://localhost:8100/build/main.js:118153:20)
at __webpack_require__ (http://localhost:8100/build/main.js:20:30)
at http://localhost:8100/build/main.js:66:18
After some googling it seems that I can not proceed that way because that is considered 'server-side' code.
Has anybody been able to add that sort of functionality on an ionic2 app?
Any help is greatly appreciated!

Related

When trying to run angular using ssr got an localize error

I'm trying to serve the angular code using this command npm run serve:ssr.
Getting the following error,
throw new Error('It looks like your application or one of its dependencies is using i18n.\n' +
^
Error: It looks like your application or one of its dependencies is using i18n.
Angular 9 introduced a global `$localize()` function that needs to be loaded.
Please run `ng add #angular/localize` from the Angular CLI.
(For non-CLI projects, add `import '#angular/localize/init';` to your `polyfills.ts` file.
For server-side rendering applications add the import to your `main.server.ts` file.)
at _global.$localize (/Users/jawad/Documents/jawad/truedealsweb/dist/server.js:68040:15)
at Module../node_modules/#ng-bootstrap/ng-bootstrap/fesm2015/ng-bootstrap.js (/Users/jawad/Documents/jawad/truedealsweb/dist/server/main.js:134281:24)
at __webpack_require__ (/Users/jawad/Documents/jawad/truedealsweb/dist/server/main.js:20:30)
at Object../node_modules/#ng-bootstrap/ng-bootstrap/ng-bootstrap.ngfactory.js (/Users/jawad/Documents/jawad/truedealsweb/dist/server/main.js:148309:12)
at __webpack_require__ (/Users/jawad/Documents/jawad/truedealsweb/dist/server/main.js:20:30)
at Object../src/app/app.server.module.ngfactory.js (/Users/jawad/Documents/jawad/truedealsweb/dist/server/main.js:275302:12)
at __webpack_require__ (/Users/jawad/Documents/jawad/truedealsweb/dist/server/main.js:20:30)
at Object../src/main.server.ts (/Users/jawad/Documents/jawad/truedealsweb/dist/server/main.js:294702:37)
at __webpack_require__ (/Users/jawad/Documents/jawad/truedealsweb/dist/server/main.js:20:30)
at Object.0 (/Users/jawad/Documents/jawad/truedealsweb/dist/server/main.js:294731:18)
Also I have add the #angular/localize in polyfills.ts but still getting same error.

DotEnv comments are crashing

I'm using dotenv version 16.0.0 in my NodeJS project but the comment feature that was recently added causes a crash. Without the comments the .env file works perfectly, loading values from it.
The .env file content:
# Print out information during runtime, useful for debugging problems not caught.
(true/false)
VERBOSE=false
# Database settings, update for actual deployment environment
DB_USERNAME=postgres
DB_PASSWORD=TINY_DUCK
DB_NAME=user_database
DB_HOST=localhost
DB_PORT=5432
The command to run the NodeJS project is:
mocha -r .env ./tests/testManager.js --exit
And the error message I get when running the NodeJS project:
× ERROR: C:\Users\thega\Source\Repos\network\.env:1
# Print out information during runtime, useful for debugging problems not caught. (true/false)
^
SyntaxError: Invalid or 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 Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at exports.requireOrImport (C:\Users\thega\source\repos\network\node_modules\mocha\lib\nodejs\esm-utils.js:60:20)
at async exports.handleRequires (C:\Users\thega\source\repos\network\node_modules\mocha\lib\cli\run-helpers.js:94:28)
at async C:\Users\thega\source\repos\network\node_modules\mocha\lib\cli\run.js:353:25
It looks to me as if you are trying to import the .env file as JS module instead of loading it with the dotenv package.
The -r flag to mocha means "require":
This will require a module before loading the user interface or test files.
It is useful for:
Test harnesses
Assertion libraries that has augment built-ins or global scope (such as should.js)
Instant ECMAScript modules using esm
Compilers like Babel via #babel/register or TypeScript using ts-node (using --require ts-node/register).
So it will try to load the file as JavaScript and of course that can't work.
Instead, you can require dotenv/config so it will parse the file for you and update process.env accordingly:
mocha -r dotenv/config ./tests/testManager.js --exit
Or, if you already do require('dotenv').config() in your code, you don't need any -r switch here at all.

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.

Using msal in Angular with Server side rendering

I've been using msal.js for authentication in my Angular application. This is working fine for development builds using ng serve. Unfortunately this library gives me an error when I try to run a server build using node. This build is generated using npm run build:prod and served by using npm run server. After attempting to serve this using node, the following error occurs:
/Users/mikey/Documents/projectname-angular8/dist/server.js:170525
class WindowWrapper extends Window {
^
ReferenceError: Window is not defined
at Module.<anonymous> (/Users/mikey/Documents/projectname-angular8/dist/server.js:170525:29)
at __webpack_require__ (/Users/mikey/Documents/projectname-angular8/dist/server.js:21:30)
at Object.#azure/msal-angular/dist/msal.module (/Users/mikey/Documents/projectname-angular8/dist/server.js:148059:18)
at __webpack_require__ (/Users/mikey/Documents/projectname-angular8/dist/server.js:141641:30)
at Object../src/app/app.server.module.ngfactory.js (/Users/mikey/Documents/projectname-angular8/dist/server.js:142963:13)
at __webpack_require__ (/Users/mikey/Documents/projectname-angular8/dist/server.js:141641:30)
at Object../src/main.server.ts (/Users/mikey/Documents/projectname-angular8/dist/server.js:147883:37)
at __webpack_require__ (/Users/mikey/Documents/projectname-angular8/dist/server.js:141641:30)
at Object.0 (/Users/mikey/Documents/projectname-angular8/dist/server.js:147904:18)
at __webpack_require__ (/Users/mikey/Documents/projectname-angular8/dist/server.js:141641:30)
I can understand why the error is happening, but as this third party library is required, I am unable to remove this package from the project all together. I've tried only loading this part of the code in app.browser.module.ts but then I am having injection errors when attempting to run the server side version in the browser.
I hope someone can explain me how I can make a runnable server side build of this project while using this third party library.

Nodejs installation error /home/ubuntu/public_html/node_test.js'

I am new to nodejs. I managed to install node.js on amazon EC2 server using this tutorial http://iwearshorts.com/blog/how-to-install-node-js-on-your-server/
But when I run the code I get this error. anyone know why this is happening and what I need to solve this.
node.js:197
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module '/home/ubuntu/public_html/node_test.js'
at Function._resolveFilename (module.js:332:15)
at Function._load (module.js:279:25)
at Array.0 (module.js:481:10)
at EventEmitter._tickCallback (node.js:188:41)
You should post your primary .js file that you are running in node so we can find your exact problem.
My guess is you are passing the wrong value to require. Pass a relative file path without the filename extension like this: var node_test = require("./public_html/node_test");

Resources