Node JS upgrade to 16.18 issue: File and Request types not found - node.js

I have upgraded from Node version 14 to node version 16.8.
λ node --version
v16.18.0
What I am seeing is, when I am running nest start --debug --watch, once the build is done successfully, it is keep on complaining the following weird stuffs. Everything was working fine with node-js 16.5.0 earlier.
Just to give you a hint, I upgraded to node js 16.5.0 to 18.x first. But as then it was decided to use node 16.18.0. So I uninstall node js in my laptop and install nodejs 16.18.0.
The errors are something like this:
import { createParamDecorator, ExecutionContext } from '#nestjs/common';
import { Request } from 'express';
export const AuthToken = createParamDecorator(
(data: unknown, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest();
return getAuthToken(request);
},
);
export const getAuthToken = (request: Request): string => {
return request.headers['authorization']?.replace('Bearer ', '');
};
And the error is:
TS2304: Cannot find name 'Request'.
8 | );
9 |
> 10 | export const getAuthToken = (request: Request): string => {
| ^^^^^^^
11 | return request.headers['authorization']?.replace('Bearer ', '');
12 | };
13 |
Similarly another code snippet:
import fs from 'fs';
import { isUndefined } from 'lodash';
import path from 'path';
export class FileUtil {
public saveMediatorLog(parentDir: File, instanceId: string, content: File) {
const srcFilePath: string = `${parentDir}/instance_${instanceId}_mediatorLog.tgz`;
const destFilePath: string = `${parentDir}/${content.name}`;
const srcFileWithAbsolutePath = path.resolve(srcFilePath);
const destFileAbsolutePath = path.resolve(destFilePath);
fs.stat(srcFilePath, function(err) {
if (isUndefined(err)) {
fs.copyFile(srcFileWithAbsolutePath, destFileAbsolutePath, (err) => {
if (err) {
throw err;
}
});
}
});
}
}
When I click on File to see the definition, it leads me to:
It has a name.
But it complains this:
TS2339: Property 'name' does not exist on type 'File'.
6 | public saveMediatorLog(parentDir: File, instanceId: string, content: File) {
7 | const srcFilePath: string = `${parentDir}/instance_${instanceId}_mediatorLog.tgz`;
> 8 | const destFilePath: string = `${parentDir}/${content.name}`;
| ^^^^
Some of my versions are:
Node Js: 16.18.0
"#types/node": "^14.14.31",
"typescript": "^4.1.5"
"ts-node": "^10.9.1",
I am really not able to understand where the issue is or what is the remedy.

For me: the issue was: yarn cache (may be some older versions were there).
So I did a forceful cache clean this:
yarn cache clean --force
rm yarn.lock
This solves the issue.
However there are quite a few dependency mismatches still exists which ideally I will fix over time.
Getting everything clean after upgrading to Node is not an easy task, seems:
Yarn Dependency errors:
warning "#nestjs/swagger > #nestjs/mapped-types#0.4.1" has incorrect peer dependency "class-transformer#^0.2.0 || ^0.3.0 || ^0.4.0".
warning " > #nestjs/typeorm#9.0.1" has incorrect peer dependency "#nestjs/common#^8.0.0 || ^9.0.0".
warning " > #nestjs/typeorm#9.0.1" has incorrect peer dependency "#nestjs/core#^8.0.0 || ^9.0.0".
warning " > #nestjs/typeorm#9.0.1" has incorrect peer dependency "rxjs#^7.2.0".
warning "#ssut/nestjs-sqs > sqs-consumer#5.7.0" has incorrect peer dependency "aws-sdk#^2.1114.0".
warning " > swagger-ui-express#4.4.0" has unmet peer dependency "express#>=4.0.0".
warning " > #nestjs/schematics#8.0.11" has incorrect peer dependency "typescript#^3.4.5 || ^4.3.5".
warning " > ts-loader#8.3.0" has unmet peer dependency "webpack#*".
A better option is to try npm install for such a case. As npm builds the dependency sequentially (yarn does it as parallel as possible), you will get step by step errors. (Of course you can override it using --legacy-peer-deps like this):
npm install --legacy-peer-deps
Without this you will get very nice errors like this:
# npm resolution error report
2022-11-05T13:47:27.128Z
While resolving: gradual-deploy#0.0.1
Found: #nestjs/common#7.6.18
node_modules/#nestjs/common
#nestjs/common#"^7.6.13" from the root project
Could not resolve dependency:
peer #nestjs/common#"^8.0.0 || ^9.0.0" from #nestjs/typeorm#9.0.1
node_modules/#nestjs/typeorm
#nestjs/typeorm#"^9.0.1" from the root project
Fix the upstream dependency conflict, or retry
this command with --force, or --legacy-peer-deps
to accept an incorrect (and potentially broken) dependency resolution.
I tried to manually resolves one by one. But the issue is: at the runtime there were so much dependency mismatches, I lost.
So I clean up the yarn cache and take a shortcut to unblock myself.

Related

React UnhandledSchemeError - "node:buffer" is not handled by plugins

I'm trying to use a package in my React project that will allow me to make API calls (axios, node-fetch, got etc.)
When these packages are not installed, the app runs properly. When any of them are installed and called in the code, I'm facing the error as follows:
Ignoring the warnings, I believe the problem has its roots from the output below:
Failed to compile.
Module build failed: UnhandledSchemeError: Reading from "node:buffer" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "node:" URIs.
I tried everything. Reinstalled node_modules. Created a clean test app, tried there. Also did my research, didn't find any relevant, clear solution on this. Nothing helped.
What am I doing wrong??
DomException file content:
/*! node-domexception. MIT License. Jimmy Wärting <https://jimmy.warting.se/opensource> */
if (!globalThis.DOMException) {
try {
const { MessageChannel } = require('worker_threads'),
port = new MessageChannel().port1,
ab = new ArrayBuffer()
port.postMessage(ab, [ab, ab])
} catch (err) {
err.constructor.name === 'DOMException' && (
globalThis.DOMException = err.constructor
)
}
}
module.exports = globalThis.DOMException
npm version: 8.5.5
node version: 16.15.0
You can work around this with this Webpack configuration:
plugins: [
new webpack.NormalModuleReplacementPlugin(/node:/, (resource) => {
resource.request = resource.request.replace(/^node:/, "");
}),
]

Domino 10 AppDevPack local installation problem

I am at this the third day now, but can't find the way to successfully use the AppDevPack on my local angular app. I am working on a Mac, I have Angular v 8.15.0. I was able to successfully install the library but when ever I wan't to compile it, it breaks.
To describe: I've done almost everything to the script. The only difference is that I have made a service in which the #domino lives(it is not directly on a component).
The main problem seems to be with grpc and then with stream.
import { Injectable } from '#angular/core';
//import { useServer } from '#domino/domino-db/';
import * as useServer from '../../../node_modules/#domino/domino-db';
#Injectable({
providedIn: 'root'
})
export class DominoService {
private serverConfig = {
hostName: 'http://www.hostname.com/',
connection: { port:'3002'}
};
private databaseConfig = {
filePath: 'dev-tmp.nsf'
};
public database: any;
constructor() {
useServer( this.serverConfig ).then( async server => {
this.database = await server.useDatabase( this.databaseConfig );
});
const coll = this.database.bulkReadDocuments({
query: "Form = 'Document'"
});
console.log("Returned docs:" + JSON.stringify(coll));
}
Here are some of the errors:
Critical dependency: the request of a dependency is an expression
WARNING in
./node_modules/#domino/domino-db/node_modules/grpc/node_modules/node-pre-gyp/lib/pre-binding.js
20:22-48 Critical dependency: the request of a dependency is an
expression
WARNING in
./node_modules/#domino/domino-db/node_modules/grpc/node_modules/node-pre-gyp/lib/util/versioning.js 17:20-67 Critical dependency: the request of a dependency is an
expression
WARNING in
./node_modules/#domino/domino-db/node_modules/grpc/node_modules/minimatch/minimatch.js
Module not found: Error: Can't resolve 'path' in
'/Users/…/node_modules/#domino/domino-db/node_modules/grpc/node_modules/minimatch'
ERROR in
./node_modules/#domino/domino-db/node_modules/grpc/node_modules/detect-libc/lib/detect-libc.js
Module not found: Error: Can't resolve 'child_process' in
‘/…/node_modules/#domino/domino-db/node_modules/grpc/node_modules/detect-libc/lib'
Error: Can't resolve 'path' in
'/Users/.../node_modules/#domino/domino-db/node_modules/grpc/node_modules/minimatch'
ERROR in
./node_modules/#domino/domino-db/node_modules/grpc/node_modules/detect-libc/lib/detect-libc.js
Module not found: Error: Can't resolve 'child_process' in
'/Users/.../node_modules/#domino/domino-db/node_modules/grpc/node_modules/detect-libc/lib'
ERROR in
./node_modules/#domino/domino-db/node_modules/grpc/src/client.js
Module not found: Error: Can't resolve 'stream' in
'/Users/.../node_modules/#domino/domino-db/node_modules/grpc/src'
Critical dependency: the request of a dependency is an expression
From the error message, I can see that you're trying to webpack this. We do not support running domino-db on a webpage. Even if you got past this error, domino-db would fail to load in that environment because it's insecure.
Domino-db in production, secure environments requires client credentials to log in. Those aren't things you want to appear in the browser page.

Use node Path module with angular 6

I'm trying to use the module Path in an Angular 6 project.
I found this post to fix the issue :
https://gist.github.com/niespodd/1fa82da6f8c901d1c33d2fcbb762947d
it says to add a script :
const fs = require('fs');
const f = 'node_modules/#angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js';
fs.readFile(f, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var result = data.replace(/node: false/g, 'node: {crypto: true, stream: true}');
fs.writeFile(f, result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
And declare it in package.json :
{...
"scripts": {
"postinstall": "node patch.js",
...
}
}
But when I'm trying to use it in a service, just import it like this :
import {join} from 'path';
It says that the module Path cannot be found.
How can I correct this ?
Interesting problem.
I managed to get Path module work on my Angular project.
Here are the steps.I use node 8, angular 6.
1: install path.
npm install path
This is an exact copy of the NodeJS ’path’ module published to the NPM registry.
2, I also installed #types/node as in Angular we are using typescript.
Although later I removed this module and path module seems still works.
3, run the above script using
node patch.js
I manually run it and go to 'node_modules/#angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js' to check the file actually changed.
4, I put
import {join} from 'path';
in one of my component.ts file
let x = join('Users', 'Refsnes', '..', 'demo_path.js');
console.log("-------------------------------------------------");
console.log(x);
in a Component's onInit() function.
and run "ng serve"
I saw the expected output in my console when loading the webpage.
-------------------------------------------------
Users/demo_path.js
So this method does work. I am not sure which step you did wrong. My guess would be the first step as I tried if not do step 3 there's different error message. Please check your node_modules folder and verify path folder exists and reinstall it if necessary.
Be sure to have node types installed: npm install --save-dev #types/node
Import path: import * as path from 'path';

node require.cache delete does not result in reload of module

I'm writing tests for my npm module.
These tests require to install multiple versions of an npm module in order to check if the module will validate them as compatible or incompatible.
Somehow all uncache libraries or function I found on stackoverflow or the npm database are not working..
I install/uninstall npm modules by using my helper functions:
function _run_cmd(cmd, args) {
return new Promise((res, rej) => {
const child = spawn(cmd, args)
let resp = ''
child.stdout.on('data', function (buffer) {
resp += buffer.toString()
})
child.stdout.on('end', function() {
res(resp)
})
child.stdout.on('error', (err) => rej(err))
})
}
global.helper = {
npm: {
install: function (module) {
return _run_cmd('npm', ['install', module])
},
uninstall: function (module) {
decacheModule(module)
return _run_cmd('npm', ['uninstall', module])
}
}
}
This is my current decache function which should clear all modules caches (I tried others, including npm modules none of them worked)
function decacheModules() {
Object.keys(require.cache).forEach(function(key) {
delete require.cache[key]
})
}
I am installing multiple versions of the less module (https://www.npmjs.com/package/less)
In my first test I am installing a deprecated version which does not have a render-function.
In some other test I am installing an up-to-date version which has the render-function. Somehow if I test for that function that test does fail.
If I skip the first test the other test succeeds. (render-function exists).
This makes me believe that the deletion of require.cache has no impact...
I am using node v4.2.4.
If there is a reference to the old module:
var less = require('less');
Clear module cache will not lead that reference cleared and reload the module.
For that to work, at least you don't store module to variable, use the "require('less')" in place everywhere.

How to use npm.commands.version to bump the package.json version programatically

I'd like the package.json version of my project to bump up every time I run my project, for that I installed the npm package into my project as a local node-module but the following does not work:
var npm = require('npm');
npm.load(function (e, n) {
n.commands.version("patch", function (error) {
// 'error' here equals an error message as shown below
console.log(error);
});
});
Error message
"npm version [ | major | minor | patch | prerelease | preminor | premajor ](run in package dir)'npm -v' or 'npm --version' to print npm version (2.4.1)'npm view version' to view a package's published version'npm ls' to inspect current package/dependency versions"
Any idea what's missing and how to make it work?
Thanks!
Btw, I don't use grunt and not planning to at this point, so grunt-bump is not an option.
Well when I read the docs, I see "Like all other commands, this function takes a string array as its first parameter", which means you need ["patch"] where you have "patch":
var npm = require('npm');
npm.load(function (e, n) {
n.commands.version(["patch"], function (error) {
// 'error' here equals an error message as shown below
console.log(error);
});
});
That works fine for me.

Resources