How to insert the .env into a folder and run the nodemon? - node.js

SERVER.JS RESUME DOTENV
const dotenv = require('dotenv-safe');
this.dotenv = dotenv.load();
Problems:
1) I can not run the nodemon if it has only the .env file, it runs only if it contains the .env and .env.example files and I would like to know why and how to correctly match it.
2) How to insert the .env in the /env folder without the problem nodemon?
3) In my start script of package.json is the following "start_dev": "nodemon app/backend/src/start.js", however it is giving the following error:
nodemon app / backend / src / start.js
[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *. *
[nodemon] starting `node app / backend / src / start.js`
consign v0.1.6 Initialized in C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel
fs.js: 115
throw err;
^
Error: ENOENT: no such file or directory, open '.env.example'
at Object.openSync (fs.js: 436: 3)
at Object.readFileSync (fs.js: 341: 35)
C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel \ node_modules \ dotenv-safe \ index.js: 27: 45)
at new Application (C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel \ app \ backend \ src \ config \ server.js: 11: 32)
at-the-object. <anonymous> (C: \ Users \ THIAGOSAAD \ Documents \ DEVELOPMENT \ NEORIS \ ALIANSCE \ aliansce-app-analyticals-panel \ app \ backend \ src \ config \ server.js: 65: 18)
at Module._compile (internal / modules / cjs / loader.js: 688: 30)
at Object.Module._extensions..js (internal / modules / cjs / loader.js: 699: 10)
at Module.load (internal / modules / cjs / loader.js: 598: 32)
at tryModuleLoad (internal / modules / cjs / loader.js: 537: 12)
at Function.Module._load (internal / modules / cjs / loader.js: 529: 3)
at Module.require (internal / modules / cjs / loader.js: 636: 17)
at require (internal / modules / cjs / helpers.js: 20: 18)
at aliasce-app-analyticals-panel \ app \ backend \ src \ start.js: 1: 78)
at Module._compile (internal / modules / cjs / loader.js: 688: 30)
at Object.Module._extensions..js (internal / modules / cjs / loader.js: 699: 10)
at Module.load (internal / modules / cjs / loader.js: 598: 32)
[nodemon] app crashed - waiting for file changes before starting ...
And if I run the nodemon in the C:\Users\username\Documents\DEVELOPMENT\NEORIS\ ALIANSCE\aliansce-app-analyticals-panel\app\ ackend\src directory
It works!

I looked at this line of code - https://github.com/rolodato/dotenv-safe/blob/master/index.js#L27
It tries to read file .env.example but can not find it in the current folder (run pwd to check it)
It might be 3 ways to solve issue
1) Run
cd app/backend/src
nodemon start.js
2) Move files .env, .env.example to parent folder (aliansce-app-analyticals-panel) and then run nodemon app/backend/src/start.js
3) Do not use dotenv-safe at all. Use your own simple script like this one
Just look at this simple example:
https://github.com/olegdovger/pizza-delivery-api/blob/master/lib/env.js (script)
https://github.com/olegdovger/pizza-delivery-api/blob/master/index.js#L1 (how to invoke script)

With help of #OlegDover and yargs, I managed to pass .env file from a different path and use nodemon for hot-reloading during development.
e.g. $ nodemon --watch /path/to/.env server.js --envPath=path/to/.env will pick up changes to .env file and restart deployment.
Example Code
.env
EXAMPLE_HOST=myhost
EXAMPLE_PORT=5566
env.js
/*
* Module dependencies
*/
const fs = require("fs");
const yargs = require("yargs");
/*
* Custom class to update process.env from custom filepath
* Ref: https://github.com/olegdovger/pizza-delivery-api/blob/master/lib/env.js
*/
class Env {
constructor(envPath) {
this.variables = [];
this._setup(envPath);
}
_setup(envPath) {
try {
const data = fs.readFileSync(envPath, {
encoding: "utf-8",
});
const stringArray = data.split("\n");
this.variables = stringArray.map((string) => {
const arr = string.split("=");
return {
name: arr[0],
value: arr[1],
};
});
} catch (err) {
console.error("Unable to load .env;", err);
}
}
load() {
this.variables.forEach((variable) => {
process.env[variable.name] = variable.value;
});
}
}
/*
* Load .env from argv filepath
*/
const argv = yargs.argv;
new Env(argv["envPath"]).load();
/**
* Register
*/
module.exports = {
EXAMPLE_HOST: process.env.EXAMPLE_HOST || "localhost",
EXAMPLE_PORT: Number(process.env.EXAMPLE_PORT) || 12345,
};
server.js
const { EXAMPLE_HOST, EXAMPLE_PORT } = require("./env");
The environment variables can then be loaded/used in the project, having the fallback values if they are not defined in the .env file.
i.e. if EXAMPLE_HOST is not present in .env, this value will default to localhost

Related

How to use the Google Closure Compiler to browserify your Node.js library

I have this simple Node.js library:
mylib/
|- inc.js
|- index.js
|- is_number.js
|- package.json
mylib/is_number.js
module.exports = x => typeof x === 'number';
mylib/inc.js
const is_number = require('./is_number');
module.exports = x => is_number(x) ? x + 1 : x;
mylib/index.js (value of the main property in my package.json)
module.exports = {
inc: require('./inc'),
utils: {
is_number: require('./is_number')
}
};
Example:
const mylib = require('mylib');
mylib.inc(41);
//=> 42
mylib.utils.is_number(42);
//=> true
How can I use the Google Closure Compiler to "browserify" my Node.js library so that it can work in a browser too? e.g.,
<script src="mylib/browser.min.js"></script>
<script>
const mylib = window.mylib;
mylib.inc(41);
//=> 42
mylib.utils.is_number(42);
//=> true
</script>
The canonical post for this answer is this Gist.
TL; DR
Create mylib/index_browser.js
window.mylib = {
inc: require('./inc'),
utils: {
is_number: require('./is_number')
}
};
Create mylib/externs.js
/** #externs */
var mylib;
var inc;
var utils;
var is_number;
Then:
$ cc --compilation_level ADVANCED \
--language_out ES5 \
--process_common_js_modules \
--module_resolution NODE \
--externs mylib/externs.js \
--isolation_mode IIFE \
--js mylib/index_browser.js mylib/inc.js mylib/is_number.js \
--js_output_file mylib/browser.min.js
Where cc is an alias to your Google Closure Compiler instance; see below for an example
Before we start:
I wrote this alias to make it easier to invoke the Google Closure Compiler (CC)
$ alias cc="java -jar /devtools/closure-compiler/compiler.jar"
$ cc --version
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20210106
The browserified version of the library will be compiled down to ES5.
Step-by-step instructions
Your first attempt might look like this: just compile the exports file mylib/index.js
$ cc --compilation_level ADVANCED \
--language_out ES5 \
--js mylib/index.js
mylib/index.js:1:0: ERROR - [JSC_UNDEFINED_VARIABLE] variable module is undeclared
1| module.exports = {
^^^^^^
mylib/index.js:2:7: ERROR - [JSC_UNDEFINED_VARIABLE] variable require is undeclared
2| inc: require('./inc'),
^^^^^^^
2 error(s), 0 warning(s)
If CC doesn't know about module and require that's not a great start.
Fortunately we're only missing the --process_common_js_modules flag:
$ cc --compilation_level ADVANCED \
--language_out ES5 \
--process_common_js_modules \
--js mylib/index.js
mylib/index.js:2:7: ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "./inc"
2| inc: require('./inc'),
^
mylib/index.js:4:15: ERROR - [JSC_JS_MODULE_LOAD_WARNING] Failed to load module "./is_number"
4| is_number: require('./is_number')
^
2 error(s), 0 warning(s)
Still not great but this time the errors are different:
CC doesn't know which require you're talking about
CC doesn't know where these two other modules are
We need the --module_resolution flag and tell CC where the other modules are:
$ cc --compilation_level ADVANCED \
--language_out ES5 \
--process_common_js_modules \
--module_resolution NODE \
--js mylib/index.js mylib/inc.js mylib/is_number.js
However the output is empty...
Why? In ADVANCED compilation mode CC removes any code that is not used. Which is the case actually: so far all this stuff isn't used at all!
Let's check with a less aggressive compilation mode:
$ cc --compilation_level WHITESPACE_ONLY --formatting PRETTY_PRINT \
--language_out ES5 \
--process_common_js_modules \
--module_resolution NODE \
--js mylib/index.js mylib/inc.js mylib/is_number.js
var module$mylib$index = {default:{}};
module$mylib$index.default.inc = module$mylib$inc.default;
module$mylib$index.default.utils = {is_number:module$mylib$is_number.default};
var module$mylib$inc = {};
var is_number$$module$mylib$inc = module$mylib$is_number.default;
module$mylib$inc.default = function(x) {
return (0,module$mylib$is_number.default)(x) ? x + 1 : x;
};
var module$mylib$is_number = {};
module$mylib$is_number.default = function(x) {
return typeof x === "number";
};
We can see that even if the ADVANCED compilation mode didn't remove everything, this wouldn't be very useful anyway. Where is window.mylib for example?
The only way I managed to get both my library available at window.mylib and compiled with the most aggressive compilation mode, is to have a separate exports file for the browser.
From this mylib/index.js
module.exports = {
inc: require('./inc'),
utils: {
is_number: require('./is_number')
}
};
To this mylib/index_browser.js
window.mylib = {
inc: require('./inc'),
utils: {
is_number: require('./is_number')
}
};
When you add to the window object CC knows that this code may be reached so it can't safely remove it anymore.
Let's try again with this file:
$ cc --compilation_level ADVANCED --formatting PRETTY_PRINT \
--language_out ES5 \
--process_common_js_modules \
--module_resolution NODE \
--js mylib/index_browser.js mylib/inc.js mylib/is_number.js
function b(a) {
return "number" === typeof a;
}
;window.g = {h:function(a) {
return b(a) ? a + 1 : a;
}, j:{i:b}};
That is looking better but there is a major problem: CC has mangled all the names!
Don't worry! We only need to tell which names CC should leave alone. That is the purpose of an externs file.
mylib/externs.js
/** #externs */
var foo;
var inc;
var utils;
var is_number;
We need another flag: --externs
$ cc --compilation_level ADVANCED --formatting PRETTY_PRINT \
--language_out ES5 \
--process_common_js_modules \
--module_resolution NODE \
--externs mylib/externs.js \
--js mylib/index_browser.js mylib/inc.js mylib/is_number.js
function b(a) {
return "number" === typeof a;
}
;window.mylib = {inc:function(a) {
return b(a) ? a + 1 : a;
}, utils:{is_number:b}};
Getting there...
One obvious improvement is to wrap all of this in an IIFE to avoid polluting the global scope more than necessary.
We need the --isolation_mode flag:
$ cc --compilation_level ADVANCED --formatting PRETTY_PRINT \
--language_out ES5 \
--process_common_js_modules \
--module_resolution NODE \
--externs mylib/externs.js \
--isolation_mode IIFE \
--js mylib/index_browser.js mylib/inc.js mylib/is_number.js
(function(){function b(a) {
return "number" === typeof a;
}
;window.mylib = {inc:function(a) {
return b(a) ? a + 1 : a;
}, utils:{is_number:b}};
}).call(this);
Fantastic!
All that is left to do is save that into a file and remove the formatting to save up a few extra bytes:
$ cc --compilation_level ADVANCED \
--language_out ES5 \
--process_common_js_modules \
--module_resolution NODE \
--externs mylib/externs.js \
--isolation_mode IIFE \
--js mylib/index_browser.js mylib/inc.js mylib/is_number.js \
--js_output_file mylib/browser.min.js
mylib/browser.min.js
(function(){function b(a){return"number"===typeof a};window.mylib={inc:function(a){return b(a)?a+1:a},utils:{is_number:b}};}).call(this);

Path name in google cloud run for Piscina

I'm added Piscina to my Node JS server, locally it works but when I upload the file to google cloud run, it's taking the directory that I specify in my Dockerfile.
In node js I use:
const piscina = new Piscina({
filename: path.resolve("./") + '/workers/send_payment_by_mail_worker.js'
});
const result = piscina.runTask({_data: this.data});
In the index.js file I can use:
const piscina = new Piscina({
filename: path.resolve( __dirname + '/workers/send_payment_by_mail_worker.js')
});
Which prints out the correct location where the file is located.
In my dockerfile I have :
FROM node:12.18.1
WORKDIR /
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install
COPY . .
CMD [ "node", "index.js" ]
Google cloud run takes / as main directory. How do you need to configure that cloud run takes the correct file directory for location the file for Piscina?
The error that I receive is:
2020-11-27T07:56:31.726607ZError [ERR_MODULE_NOT_FOUND]: Cannot find module '/services/action/workers/send_payment_by_mail_worker.js' imported from /node_modules/piscina/dist/src/worker.js
Default
2020-11-27T07:56:31.726628Z at finalizeResolution (internal/modules/esm/resolve.js:284:11)
Default
2020-11-27T07:56:31.726636Z at moduleResolve (internal/modules/esm/resolve.js:665:10)
Default
2020-11-27T07:56:31.726642Z at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:755:11)
Default
2020-11-27T07:56:31.726648Z at Loader.resolve (internal/modules/esm/loader.js:97:40)
Default
2020-11-27T07:56:31.726654Z at Loader.getModuleJob (internal/modules/esm/loader.js:243:28)
Default
2020-11-27T07:56:31.726660Z at Loader.import (internal/modules/esm/loader.js:178:28)
Default
2020-11-27T07:56:31.726666Z at importModuleDynamically (internal/modules/cjs/loader.js:1081:27)
Default
2020-11-27T07:56:31.726671Z at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:37:14)
Default
2020-11-27T07:56:31.726678Z at eval (eval at getImportESM (/node_modules/piscina/dist/src/worker.js:37:27), <anonymous>:1:16)
Default
2020-11-27T07:56:31.726684Z at getHandler (/node_modules/piscina/dist/src/worker.js:58:39)
Default
2020-11-27T07:56:31.726919ZuncaughtException

nodejs webapp works locally but not live/deployed

My webapp works locally, but when I attempt to publish it on Azure Web Apps the following error pops up. It is showing Error: listen EADDRINUSE: address already in use :::8080, but the webapp should be using port 3000. I am new to creating webapp, so any help with be appreciated. I used visual studio code to publish the webapp following these settings - https://learn.microsoft.com/en-us/azure/developer/javascript/tutorial-vscode-azure-app-service-node-03. I chose linux and node 12 LTS.
Thank you!
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('myapp:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
2020-07-16T04:23:44.555179603Z / _ \ __________ _________ ____
2020-07-16T04:23:44.555185003Z / /_\ \___ / | \_ __ \_/ __ \
2020-07-16T04:23:44.555189203Z / | \/ /| | /| | \/\ ___/
2020-07-16T04:23:44.555193303Z \____|__ /_____ \____/ |__| \___ >
2020-07-16T04:23:44.555197403Z \/ \/ \/
2020-07-16T04:23:44.555201303Z A P P S E R V I C E O N L I N U X
2020-07-16T04:23:44.555205203Z
2020-07-16T04:23:44.555208803Z Documentation: http://aka.ms/webapp-linux
2020-07-16T04:23:44.555212503Z NodeJS quickstart: https://aka.ms/node-qs
2020-07-16T04:23:44.555216303Z NodeJS Version : v12.16.3
2020-07-16T04:23:44.555220003Z Note: Any data outside '/home' is not persisted
2020-07-16T04:23:44.555223903Z
2020-07-16T04:23:44.902776147Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2020-07-16T04:23:44.912621757Z Build Operation ID: |bLCEuDLkBSY=.df68a68e_
2020-07-16T04:23:46.477381888Z Writing output script to '/opt/startup/startup.sh'
2020-07-16T04:23:47.136900207Z Running #!/bin/sh
2020-07-16T04:23:47.137528808Z
2020-07-16T04:23:47.137541508Z # Enter the source directory to make sure the script runs where the user expects
2020-07-16T04:23:47.137547108Z cd "/home/site/wwwroot"
2020-07-16T04:23:47.137551508Z
2020-07-16T04:23:47.137555608Z export NODE_PATH=$(npm root --quiet -g):$NODE_PATH
2020-07-16T04:23:47.138703711Z if [ -z "$PORT" ]; then
2020-07-16T04:23:47.138716311Z export PORT=8080
2020-07-16T04:23:47.138721811Z fi
2020-07-16T04:23:47.138725911Z
2020-07-16T04:23:47.139711413Z echo Found tar.gz based node_modules.
2020-07-16T04:23:47.139723313Z extractionCommand="tar -xzf node_modules.tar.gz -C /node_modules"
2020-07-16T04:23:47.139728913Z echo "Removing existing modules directory from root..."
2020-07-16T04:23:47.139733513Z rm -fr /node_modules
2020-07-16T04:23:47.139737613Z mkdir -p /node_modules
2020-07-16T04:23:47.139741713Z echo Extracting modules...
2020-07-16T04:23:47.141077216Z $extractionCommand
2020-07-16T04:23:47.141089216Z export NODE_PATH="/node_modules":$NODE_PATH
2020-07-16T04:23:47.141094416Z export PATH=/node_modules/.bin:$PATH
2020-07-16T04:23:47.141098616Z if [ -d node_modules ]; then
2020-07-16T04:23:47.142105319Z mv -f node_modules _del_node_modules || true
2020-07-16T04:23:47.142117019Z fi
2020-07-16T04:23:47.142122019Z
2020-07-16T04:23:47.142126219Z if [ -d /node_modules ]; then
2020-07-16T04:23:47.142140519Z ln -sfn /node_modules ./node_modules
2020-07-16T04:23:47.142145419Z fi
2020-07-16T04:23:47.149224835Z
2020-07-16T04:23:47.149263335Z echo "Done."
2020-07-16T04:23:47.149334335Z npm start
2020-07-16T04:23:48.155357951Z Found tar.gz based node_modules.
2020-07-16T04:23:48.155942252Z Removing existing modules directory from root...
2020-07-16T04:23:48.164530572Z Extracting modules...
2020-07-16T04:23:51.370781353Z Done.
2020-07-16T04:23:52.204989273Z npm info it worked if it ends with ok
2020-07-16T04:23:52.206185276Z npm info using npm#6.14.4
2020-07-16T04:23:52.206929677Z npm info using node#v12.16.3
2020-07-16T04:23:52.736398696Z npm info lifecycle myapp#0.0.0~prestart: myapp#0.0.0
2020-07-16T04:23:52.746327019Z npm info lifecycle myapp#0.0.0~start: myapp#0.0.0
2020-07-16T04:23:52.759340349Z
2020-07-16T04:23:52.759356549Z > myapp#0.0.0 start /home/site/wwwroot
2020-07-16T04:23:52.759362549Z > node ./bin/www
2020-07-16T04:23:52.759366849Z
2020-07-16T04:23:54.160267671Z Express server listening on port 8080
2020-07-16T04:23:54.170115279Z events.js:287
2020-07-16T04:23:54.170141079Z throw er; // Unhandled 'error' event
2020-07-16T04:23:54.170147979Z ^
2020-07-16T04:23:54.170152379Z
2020-07-16T04:23:54.170156779Z Error: listen EADDRINUSE: address already in use :::8080
2020-07-16T04:23:54.170161379Z at Server.setupListenHandle [as _listen2] (net.js:1313:16)
2020-07-16T04:23:54.170165779Z at listenInCluster (net.js:1361:12)
2020-07-16T04:23:54.170170179Z at Server.listen (net.js:1449:7)
2020-07-16T04:23:54.170174479Z at module.exports (/home/site/wwwroot/routes/table_routes2.js:63:27)
2020-07-16T04:23:54.170178979Z at Object. (/home/site/wwwroot/app.js:119:37)
2020-07-16T04:23:54.170183679Z at Module._compile (internal/modules/cjs/loader.js:1133:30)
2020-07-16T04:23:54.170187979Z at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
2020-07-16T04:23:54.170192279Z at Module.load (internal/modules/cjs/loader.js:977:32)
2020-07-16T04:23:54.170196679Z at Function.Module._load (internal/modules/cjs/loader.js:877:14)
2020-07-16T04:23:54.170200879Z at Module.require (internal/modules/cjs/loader.js:1019:19)
2020-07-16T04:23:54.170205179Z at require (internal/modules/cjs/helpers.js:77:18)
2020-07-16T04:23:54.170209379Z at Object. (/home/site/wwwroot/bin/www:7:11)
2020-07-16T04:23:54.170213779Z at Module._compile (internal/modules/cjs/loader.js:1133:30)
2020-07-16T04:23:54.170218079Z at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
2020-07-16T04:23:54.170231979Z at Module.load (internal/modules/cjs/loader.js:977:32)
2020-07-16T04:23:54.170236679Z at Function.Module._load (internal/modules/cjs/loader.js:877:14)
2020-07-16T04:23:54.170240879Z Emitted 'error' event on Server instance at:
2020-07-16T04:23:54.170244979Z at emitErrorNT (net.js:1340:8)
2020-07-16T04:23:54.170248979Z at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-07-16T04:23:54.170253279Z code: 'EADDRINUSE',
2020-07-16T04:23:54.170257279Z errno: 'EADDRINUSE',
2020-07-16T04:23:54.170261279Z syscall: 'listen',
2020-07-16T04:23:54.170265279Z address: '::',
2020-07-16T04:23:54.170269379Z port: 8080
2020-07-16T04:23:54.170273379Z }
2020-07-16T04:23:54.198841600Z npm info lifecycle myapp#0.0.0~start: Failed to exec start script
2020-07-16T04:23:54.200937601Z npm ERR! code ELIFECYCLE
2020-07-16T04:23:54.201865802Z npm ERR! errno 1
2020-07-16T04:23:54.203466603Z npm ERR! myapp#0.0.0 start: `node ./bin/www`
2020-07-16T04:23:54.203976603Z npm ERR! Exit status 1
2020-07-16T04:23:54.204590604Z npm ERR!
2020-07-16T04:23:54.205116404Z npm ERR! Failed at the myapp#0.0.0 start script.
2020-07-16T04:23:54.211395609Z npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-16T04:23:54.346290903Z npm timing npm Completed in 2399ms
2020-07-16T04:23:54.347057103Z
2020-07-16T04:23:54.347655604Z npm ERR! A complete log of this run can be found in:
2020-07-16T04:23:54.436761954Z npm ERR! /root/.npm/_logs/2020-07-16T04_23_54_212Z-debug.log
try without normalizeport function

how do i fix issue regarding "npm -v" not working? I upgrade to angular 10 and everytime I use terminal this below error

how do i fix issue regarding "npm -v" not working? I upgrade to angular 10 and everytime I use terminal this below error.
internal / modules / cjs / loader.js: 976
throw err; ^
Error: Cannot find module '../lib/utils/unsupported.js'
Require stack:
-/usr/local / lib / node_modules / npm / bin / npm - cli.js
at Function.Module._resolveFilename(internal / modules / cjs / loader.js: 973: 15)
at Function.Module._load(internal / modules / cjs / loader.js: 855: 27)
at Module.require(internal / modules / cjs / loader.js: 1033: 19)
at require(internal / modules / cjs / helpers.js: 72: 18)
at / usr / local / lib / node_modules / npm / bin / npm - cli.js: 19: 21
at Object. < anonymous > (/usr/local / lib / node_modules / npm / bin / npm - cli.js: 153: 3)
at Module._compile(internal / modules / cjs / loader.js: 1144: 30)
at Object.Module._extensions..js(internal / modules / cjs / loader.js: 1164: 10)
at Module.load(internal / modules / cjs / loader.js: 993: 32)
at Function.Module._load(internal / modules / cjs / loader.js: 892: 14) {
code: 'MODULE_NOT_FOUND',
requireStack: ['/usr/local/lib/node_modules/npm/bin/npm-cli.js']
}
`

net::ERR_ADDRESS_UNREACHABLE at {URL}

i am using puppeteer v1.19.0 in nodejs , is error unreachable after build and run in docker,
is js file
await puppeteer.launch({
executablePath: '/usr/bin/chromium-browser',
args: ['--no-sandbox', '--disable-setuid-sandbox', '--headless'],
}).then(async (browser) => {
const url = `${thisUrl}analisa-jabatan/pdf/${_id}`
const page = await browser.newPage()
await page.goto(url, { waitUntil: 'networkidle0' })
// await page.evaluate(() => { window.scrollBy(0, window.innerHeight) })
await page.setViewport({
width: 1123,
height: 794,
})
setTimeout(async () => {
const buffer = await page.pdf({
path: `uploads/analisa-jabatan.pdf`,
displayHeaderFooter: true,
headerTemplate: '',
footerTemplate: '',
printBackground: true,
format: 'A4',
landscape: true,
margin: {
top: 20,
bottom: 20,
left: 20,
right: 20,
},
})
let base64data = buffer.toString('base64')
await res.status(200).send(base64data)
// await res.download(process.cwd() + '/uploads/analisa-jabatan.pdf')
await browser.close()
}, 2000)
})
}
and is dockerfile
FROM aria/alpine-nodejs:3.10
#FROM node:12-alpine
LABEL maintainer="Aria <aryamuktadir22#gmail.com>"
# ENVIRONMENT VARIABLES
# NODE_ENV
ENV NODE_ENV=production
# SERVER Configuration
ENV HOST=0.0.0.0
ENV PORT=3001
ENV SESSION_SECRET=thisissecret
# CORS Configuration
ENV CORS_ORIGIN=http://117.54.250.109:8081
ENV CORS_METHOD=GET,POST,PUT,DELETE,PATCH,OPTIONS,HEAD
ENV CORS_ALLOWED_HEADERS=Authorization,Content-Type,Access-Control-Request-Method,X-Requested-With
ENV CORS_MAX_AGE=600
ENV CORS_CREDENTIALS=false
# DATABASE Configuration
ENV DB_HOST=anjabdb
ENV DB_PORT=27017
ENV DB_NAME=anjab
# Tell Puppeteer to skip installing Chrome. We'll be using the installed package.
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
# SET WORKDIR
WORKDIR /usr/local/app
# INSTALL REQUIRED DEPENDENCIES
RUN apk update && apk upgrade && \
apk add --update --no-cache \
gcc g++ make autoconf automake pngquant \
python2 \
chromium \
udev \
nss \
freetype \
freetype-dev \
harfbuzz \
ca-certificates \
ttf-freefont ca-certificates \
nodejs \
yarn \
libpng libpng-dev lcms2 lcms2-dev
# COPY SOURCE TO CONTAINER
ADD deploy/etc/ /etc
ADD package.json app.js server.js process.yml ./
ADD lib ./lib
ADD middlewares ./middlewares
ADD models ./models
ADD modules ./modules
ADD uploads ./uploads
ADD assets ./assets
ADD views ./views
COPY keycloak.js.prod ./keycloak.js
# INSTALL NODE DEPENDENCIES
RUN npm cache clean --force
RUN npm config set unsafe-perm true
RUN npm -g install pm2 phantomjs html-pdf
RUN yarn && yarn install --production=true && sleep 3 &&\
yarn cache clean
RUN set -ex \
&& apk add --no-cache --virtual .build-deps ca-certificates openssl \
&& wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1/dockerized-phantomjs.tar.gz" | tar xz -C / \
&& npm install -g phantomjs \
&& apk del .build-deps
EXPOSE 3001
And is result
Error: net::ERR_ADDRESS_UNREACHABLE at http://117.54.250.109:8089/analisa-jabatan/pdf/5ee9e6a15ff81d00c7c3a614
at navigate (/usr/local/app/node_modules/puppeteer/lib/FrameManager.js:120:37)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at Frame. (/usr/local/app/node_modules/puppeteer/lib/helper.js:111:15)
at Page.goto (/usr/local/app/node_modules/puppeteer/lib/Page.js:674:49)
at Page. (/usr/local/app/node_modules/puppeteer/lib/helper.js:112:23)
at puppeteer.launch.then (/usr/local/app/modules/analisajabatan/methods/pdfpuppeteer.js:60:20)
at process._tickCallback (internal/process/next_tick.js:68:7)
From the puppeteer docs:
page.goto will not throw an error when any valid HTTP status code is returned by the remote server, including 404 "Not Found" and 500 "Internal Server Error"
so providing the url is valid, the server doesn't seem to be sending a response.

Resources