Angular5 webpack ReferenceError: window is not defined - node.js

I am facing windows not defined error while trying to configure my angluar2 application with angular-universal.
cli version is migrated from 1.7.4 -> 6.0.3
Below is the error i am getting.
E:\Personal\my_codes\dist\server.js:1721
*/},function(e,t,n){"use strict";n.r(t),n.d(t,"provideModuleMap",function(){return a}),n.d(t,"ModuleMapLoaderModule",function(){return s}),n.d(t,"MODULE_MAP",function(){return i}),n.d(t,"ModuleMapNgFactoryLoader",function(){return o});var r=n(0),i=new r.InjectionToken("MODULE_MAP"),o=function(){function e(e,t){this.compiler=e,this.moduleMap=t}return e.prototype.load=function(e){var t=this.compiler instanceof r.Compiler,n=this.moduleMap[e];if(!n)throw new Error(e+" did not exist in the MODULE_MAP");return t?this.loadFactory(n):this.loadAndCompile(n)},e.prototype.loadFactory=function(e){return new Promise(function(t){return t(e)})},e.prototype.loadAndCompile=function(e){return this.compiler.compileModuleAsync(e)},e.decorators=[{type:r.Injectable}],e.ctorParameters=function(){return[{type:r.Compiler},{type:void 0,decorators:[{type:r.Inject,args:[i]}]}]},e}();function a(e){return{provide:i,useValue:e}}var s=function(){function e(){}return e.withMap=functio
ReferenceError: window is not defined
at Object.<anonymous> (E:\Personal\my_codes\dist\server.js:1721:1147)
at n (E:\Personal\my_codes\dist\server.js:1:172)
at Object.<anonymous> (E:\Personal\my_codes\dist\server.js:2001:2400)
at n (E:\Personal\my_codes\dist\server.js:1:172)
at E:\Personal\my_codes\dist\server.js:1:579
at Object.<anonymous> (E:\Personal\my_codes\dist\server.js:1:590)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! col-ui#0.0.0 serve:ssr: `node dist/server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the col-ui#0.0.0 serve:ssr script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
Any suggestion on how i can resolve this will be really helpful.

Try to use domino , that will support many inconsistency in Angular SSR.
In server.ts
const domino = require('domino');
const win = domino.createWindow(template);
global['window'] = win;
global['document'] = win.document;
global['navigator'] = win.navigator;
global['CSS'] = undefined;

Related

EPERM while using bower

I get an error everytime I try to use bower even the bower command pop me an error
bower install bootstrap
C:\Users\laptop\AppData\Roaming\npm\node_modules\bower\lib\node_modules\configstore\node_modules\mkdirp\index.js:90
throw err0;
^
Error: EPERM: operation not permitted, mkdir 'C:\Users\laptop.config'
at Object.mkdirSync (node:fs:1334:3)
at sync (C:\Users\laptop\AppData\Roaming\npm\node_modules\bower\lib\node_modules\configstore\node_modules\mkdirp\index.js:71:13)
at Function.sync (C:\Users\laptop\AppData\Roaming\npm\node_modules\bower\lib\node_modules\configstore\node_modules\mkdirp\index.js:77:24)
at Configstore.get (C:\Users\laptop\AppData\Roaming\npm\node_modules\bower\lib\node_modules\configstore\index.js:39:13)
at new Configstore (C:\Users\laptop\AppData\Roaming\npm\node_modules\bower\lib\node_modules\configstore\index.js:28:45)
at readCachedConfig (C:\Users\laptop\AppData\Roaming\npm\node_modules\bower\lib\config.js:19:23)
at defaultConfig (C:\Users\laptop\AppData\Roaming\npm\node_modules\bower\lib\config.js:11:12)
at Object. (C:\Users\laptop\AppData\Roaming\npm\node_modules\bower\lib\index.js:16:32)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) {
errno: -4048,
syscall: 'mkdir',
code: 'EPERM',
path: 'C:\Users\laptop\.config'
}

Algorithm "brotliCompress" is not found in "zlib"

I have installed compression-webpack-plugin using this link and added Compression plugin in plugin section of webpack.config.js file as suggested here
When I run npm run build I am getting an error
Algorithm "brotliCompress" is not found in "zlib"
I am using node v8.11.3.
Here is the full error
C:\Projects\ProjName\FrontEnd\node_modules\webpack-cli\bin\cli.js:93
throw err;
^
Error: Algorithm "brotliCompress" is not found in "zlib"
at new CompressionPlugin (C:\Projects\ProjName\FrontEnd\node_modules\compression-webpack-plugin\dist\index.js:171:15)
at Object. (C:\Projects\ProjName\FrontEnd\webpack\webpack.config.prod.js:67:5)
at Module._compile (C:\Projects\ProjName\FrontEnd\node_modules\v8-compile-cache\v8-compile-cache.js:192:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (C:\Projects\ProjName\FrontEnd\node_modules\v8-compile-cache\v8-compile-cache.js:159:20)
at WEBPACK_OPTIONS (C:\Projects\ProjName\FrontEnd\node_modules\webpack-cli\bin\utils\convert-argv.js:114:13)
at requireConfig (C:\Projects\ProjName\FrontEnd\node_modules\webpack-cli\bin\utils\convert-argv.js:116:6)
at C:\Projects\ProjName\FrontEnd\node_modules\webpack-cli\bin\utils\convert-argv.js:123:17
at Array.forEach ()
at module.exports (C:\Projects\ProjName\FrontEnd\node_modules\webpack-cli\bin\utils\convert-argv.js:121:15)
at yargs.parse (C:\Projects\ProjName\FrontEnd\node_modules\webpack-cli\bin\cli.js:71:45)
at Object.parse (C:\Projects\ProjName\FrontEnd\node_modules\webpack-cli\node_modules\yargs\yargs.js:576:18)
at C:\Projects\ProjName\FrontEnd\node_modules\webpack-cli\bin\cli.js:49:8
at Object. (C:\Projects\ProjName\FrontEnd\node_modules\webpack-cli\bin\cli.js:366:3)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object. (C:\Projects\ProjName\FrontEnd\node_modules\webpack\bin\webpack.js:156:2)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3 npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! ProjName#1.0.0 build: npm run buildsprite && webpack --config ./webpack/webpack.config.prod.js --colors npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the ProjName#1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely
additional logging output above.
npm ERR! A complete log of this run can be found in: npm ERR!
C:\Users\2048537\AppData\Roaming\npm-cache_logs\2021-12-29T09_59_46_237Z-debug.log
This is how I injected the plugin
new CompressionPlugin({
filename: "[path][base].br",
algorithm: "brotliCompress",
test: /\.(js|css|html|svg)$/,
compressionOptions: {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 11,
},
},
threshold: 10240,
minRatio: 0.8,
deleteOriginalAssets: false,
}),
Looks like webpack team needs to update https://webpack.js.org/plugins/compression-webpack-plugin/#using-brotli link.
I replace plugin code as below as below
new BrotliGzip({
asset: '[file].br[query]',
algorithm: "brotli",
test: /\.(js|css|html|svg)$/,
threshold: 10240,
minRatio: 0.8,
quality: 11
}),
where BrotliGzip is defined as
const BrotliGzip = require("brotli-gzip-webpack-plugin");
then it worked.

youtube-dl-exec issue - get readable stream

Im trying to get a readble stream from youtube-dl-exec.
youtubedl(url, {
dumpSingleJson: true,
noWarnings: true,
audioFormat: 'mp3',
//audioQuality: 0,
noCheckCertificate: true,
preferFreeFormats: true,
youtubeSkipDashManifest: true,
referer: 'https://google.com'
})
.then(async output => {
let resource = createAudioResource(output); // output = YtResponse (JSON)
})
.catch(err => console.error(err));
output is typeof YtResponse (JSON with formats and urls, thumbnails etc.)
So with
let url = output.formats[0]?.url;
I can get an URL like
https://r1---sn-h0jelnez.googlevideo.com/videoplayback?expire=
But I can't stream this because it's not a readable stream..
So I tried to use youtube-dl library. But if I wan't to install it, i get this error:
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c npm run check && npm run download
npm ERR! > youtube-dl#3.5.0 check
npm ERR! > bin-version-check python '>=2'
npm ERR! Error: Invalid version range
npm ERR! at module.exports (C:\Users\Admin\Documents\Nodejs\LL-Bot-Musik\node_modules\bin-version-check\index.js:12:25)
npm ERR! at Object.<anonymous> (C:\Users\Admin\Documents\Nodejs\LL-Bot-Musik\node_modules\bin-version-check-cli\cli.js:29:1)
npm ERR! at Module._compile (node:internal/modules/cjs/loader:1101:14)
npm ERR! at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
npm ERR! at Module.load (node:internal/modules/cjs/loader:981:32)
npm ERR! at Function.Module._load (node:internal/modules/cjs/loader:822:12)
npm ERR! at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
npm ERR! at node:internal/main/run_main_module:17:47
npm ERR! A complete log of this run can be found in:
Now I don't know what I can do..

Related to testing framework- Mocha

I am developing simple DApps using Ethereum platform. And in this, I have used Mocha test framework but coming with this error. Please help...
E:\Tutorials\Blockchain\Practicle\Demo>npm run test
demo#1.0.0 test E:\Tutorials\Blockchain\Practicle\Demo
mocha
TypeError: Suite argument "title" must be a string. Received type "function"
at createInvalidArgumentTypeError (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\errors.js:158:13)
at new Suite (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\suite.js:45:15)
at Function.Suite.create (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\suite.js:26:17)
at Object.create (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\interfaces\common.js:128:27)
at context.describe.context.context (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\interfaces\bdd.js:42:27)
at Object.<anonymous> (E:\Tutorials\Blockchain\Practicle\Demo\test\inbox_test.js:13:1)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Module.require (internal/modules/cjs/loader.js:1026:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.exports.requireOrImport (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\esm-utils.js:20:12)
at Object.exports.loadFilesAsync (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\esm-utils.js:33:34)
at Mocha.loadFilesAsync (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\mocha.js:421:19)
at singleRun (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\cli\run-helpers.js:156:15)
at exports.runMocha (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\cli\run-helpers.js:225:10)
at Object.exports.handler (E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\lib\cli\run.js:366:11)
at E:\Tutorials\Blockchain\Practicle\Demo\node_modules\mocha\node_modules\yargs\lib\command.js:241:49
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! demo#1.0.0 test: `mocha`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the demo#1.0.0 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Prabhakar's\AppData\Roaming\npm-cache\_logs\2020-06-21T05_32_41_476Z-debug.log
I got my answer. And I m posting it so that it can be helpful others...
Earlier I have defined my describe method as:
describe(() => {
it("can pass the test", () => {
console.log(accounts);
});
});
Error Image
and here direct function I have passed instead of going for tittle first that's why mocha wasn't able to make it and return with error :
Suite argument "title" must be a string. Received type "function"
But after giving my contract method it accepts it as a tittle
describe( 'inbox', () => {
it("can pass the test", () => {
console.log(accounts);
});
});
Final Image

npm --version throws an error on Windows 7

When executing npm --version on Windows 7 x64, I get:
net.js:10
const cares = process.binding('cares_wrap');
^
Error: EFILE
at net.js:10:23
at NativeModule.compile (bootstrap_node.js:523:7)
at NativeModule.require (bootstrap_node.js:464:18)
at createWritableStdioStream (internal/process/stdio.js:155:19)
at process.getStdout [as stdout] (internal/process/stdio.js:10:14)
at module.exports (C:\Program Files\nodejs\node_modules\npm\node_modules\npm
log\node_modules\set-blocking\index.js:2:11)
at Object.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules
\npmlog\log.js:11:1)
at Module._compile (module.js:573:32)
at Object.Module._extensions..js (module.js:582:10)
at Module.load (module.js:490:32)
net.js:10
const cares = process.binding('cares_wrap');
^
Error: EFILE
at net.js:10:23
at NativeModule.compile (bootstrap_node.js:523:7)
at NativeModule.require (bootstrap_node.js:464:18)
at tty.js:4:13
at NativeModule.compile (bootstrap_node.js:523:7)
at NativeModule.require (bootstrap_node.js:464:18)
at createWritableStdioStream (internal/process/stdio.js:142:19)
at process.getStdout [as stdout] (internal/process/stdio.js:10:14)
at module.exports (C:\Program Files\nodejs\node_modules\npm\node_modules\npm
log\node_modules\set-blocking\index.js:2:11)
at Object.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules
\npmlog\log.js:11:1)
May this be that that computer is a Virtual Machine ? On the other hand, the node --version command give a correct output.

Resources