jhipster based nhipster server start error - node.js

I am trying to generate Angular NestJS based application using Jhipster i.e, nhipster.
Below is my jhipster and nodejs version numbers:
Jhipster : 7.7.0
NodeJS: 16.14.0 LTS
But I am getting the below error, when I am trying to start the server:
Error:
/projects/nhipster/mauto/server/node_modules/ts-node/src/index.ts:240
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/client/header-util.ts:56:19 - error TS2552: Cannot find name 'URL'. Did you mean 'url'?
56 url = new URL('http://localhost' + url);
~~~
src/client/header-util.ts:55:32
55 private static prepareLink(url, pageNumber, pageSize, relType): any {
~~~
'url' is declared here.
at createTSError (/Users/kmadasu/kishore/mnkb/projects/nhipster/mauto/server/node_modules/ts-node/src/index.ts:240:12)
at reportTSError (/Users/kmadasu/kishore/mnkb/projects/nhipster/mauto/server/node_modules/ts-node/src/index.ts:244:19)
at getOutput (/Users/kmadasu/kishore/mnkb/projects/nhipster/mauto/server/node_modules/ts-node/src/index.ts:360:34)
at Object.compile (/Users/kmadasu/kishore/mnkb/projects/nhipster/mauto/server/node_modules/ts-node/src/index.ts:393:11)
at Module.m._compile (/Users/kmadasu/kishore/mnkb/projects/nhipster/mauto/server/node_modules/ts-node/src/index.ts:439:43)
at Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Object.require.extensions.<computed> [as .ts] (/Users/kmadasu/kishore/mnkb/projects/nhipster/mauto/server/node_modules/ts-node/src/index.ts:442:12)
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)
[nodemon] app crashed - waiting for file changes before starting...

That "URL" function at your line "56" is not imported into the typescript module properly. It is a function that comes from dom definition inside the typescript compiler. You could find a way to import it into the header-util class. Another way, and what I'd do is enable "dom" in the compiler options in the ts-config file of the server, by adding "dom" to the lib array like so:
{
"compilerOptions": {
/* Basic Options */
"typeRoots": ["node_modules/#types"],
"target": "ES2017" /* Specify ECMAScript target version: 'ES3' (default),
'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs',
'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"es6",
"dom" /* <--- here you are */
]
...
}

Related

Issue with JoinTable in NestJS + TypeORM

I'm struggling with a weird issue in NestJS + TypeORM.
Pretty much I've created a ManyToMany relation in an entity and, on the owning side of the relation, I've added the #JoinTable() with no parameters.
After running nest build, the entity.js file adds a new import const browser_1 = require("typeorm/browser");, which, I found out, is used in declaring the JoinTable option in the compiled JS file => browser_1.JoinTable().
The issue is that, when using typeorm to generate a new migration file, I keep getting the following error:
import { __awaiter, __generator } from "tslib";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:979:16)
at Module._compile (internal/modules/cjs/loader.js:1027:27)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .js] (C:\XXX\XXX\dev\XXX\pistis-api\node_modules\ts-node\src\index.ts:1045:43)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\XXX\XXX\dev\XXX\pistis-api\dist\modules\collaborator\entity\collaborator.entity.js:17:19)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
Tracking down the issue, it seems to be related to this browser import that is being imported from ./node_modules/typeorm/browser/index.js
I'm using .env to configure typeorm, with the following:
TYPEORM_CONNECTION=postgres
TYPEORM_HOST=localhost
TYPEORM_USERNAME=xxxxx
TYPEORM_PASSWORD=xxxxx
TYPEORM_DATABASE=xxxx
TYPEORM_PORT=5432
TYPEORM_SYNCHRONIZE=false
TYPEORM_LOGGING=true
TYPEORM_DROP_SCHEMA=false
TYPEORM_MIGRATIONS_RUN=true
TYPEORM_ENTITIES=dist/modules/**/entity/*.js
TYPEORM_ENTITIES_DIR=src/modules/**/entity
TYPEORM_MIGRATIONS=dist/migrations/*.js
TYPEORM_MIGRATIONS_DIR=src/migrations
TYPEORM_MIGRATIONS_TABLE_NAME='orm_migrations'
This configuration has worked until I've introduced the ManyToMany relation and the JoinTable.
As far as .tsconfig goes, I have:
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [
"dom",
"es6",
"dom.iterable",
"esnext"
],
"allowJs": true,
"declaration": true,
"removeComments": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
},
"include": [
"src"
]
}
Any help would be appreciated.
Thanks in advance!
So, after a month of not touching this project, I decided to restart it and take a fresh look into it. Pretty much came to find out it was a newbie issue related to the way I'm importing the JoinTable option. I pretty much trusted the IDE to do the import and that was my mistake.
So, to sum it up:
I had:
import {JoinTable} from 'typeorm/browser';
When I should have:
import {JoinTable} from 'typeorm';
For future reference, these import errors might also originate from simple mistakes like these and not the complex ones we find on Stackoverflow telling us to check tsconfig.json and the likes.
Thanks :)

NodeJS-Typescript-Yarn : Error: Cannot find module 'd3'

I'm getting this error and couldn't resolve d3 import . I'm using Typescript#4.0.5 and Yarn
The file Layout.ts (Layout.js) imports d3 which throws an error.
import * as D3 from "d3";
and yarn build
$ yarn build
yarn run v1.22.5
$ tsc && node dist/lib/index.js
internal/modules/cjs/loader.js:834
throw err;
^
Error: Cannot find module 'd3'
Require stack:
- /home/github/my-app/dist/lib/graphs/Layout.js
- /home/github/my-app/dist/lib/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:831:15)
at Function.Module._load (internal/modules/cjs/loader.js:687:27)
at Module.require (internal/modules/cjs/loader.js:903:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/home/github/my-app/dist/lib/graphs/Layout.js:27:25)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:903:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/github/my-app/dist/lib/graphs/Layout.js',
'/home/github/my-app/dist/lib/index.js'
]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
and my tsconfig,
{
"compileOnSave": true,
"compilerOptions": {
"target": "es2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"lib": ["DOM","dom.iterable","ES2018.AsyncIterable","ES2017.TypedArrays","es2018", "esnext.asynciterable", "ES5", "ES6"], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"declarationMap": true,
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"types": [
"node", /* Type declaration files to be included in compilation. */
],
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
/* Advanced Options */
//"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
any help is much appreciated. Thanks in advance.
There is in built in command build in yarn.
All possible options you can find here.
Try yarn install instead
After a few days, gave it a try again, found a solution for adding type d3,
for old versions,
add globals
yarn install typings --global.
search for typings named d3
yarn typings search --name d3
if not exist install it,
yarn typings install d3 --save. //for old versions
yarn add #types/d3. //latest versions
and it worked as expected!

Declaration merging doesn't work with express 4.17.* Request type

I want to add a property to the Request type, so I created a folder #types/express and in this folder I've added file index.d.ts with this content.
namespace Express {
interface Request {
user: number;
}
}
In VSCode the error has gone while I'm referencing to req.user, and it even shows that user is of type number
screenshot that shows that "user" property on the "Request" object is treated right
but when I start the server I see the error that says this:
/home/myself/web/my-server/node_modules/ts-node/src/index.ts:434
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/app.ts:46:7 - error TS2339: Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs>'.
46 req.user;
~~~~
at createTSError (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:434:12)
at reportTSError (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:438:19)
at getOutput (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:578:36)
at Object.compile (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:775:32)
at Module.m._compile (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:858:43)
at Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Object.require.extensions.<computed> [as .ts] (/home/myself/web/my-server/node_modules/ts-node/src/index.ts:861:12)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
I'd appreciate any ideas on how to fix it.
p.s. I have done the same with express-session module and added a counter property into the Session interface, and it works flawlessly
With #types/express installed (#types/express-session is sufficient, too), this should work:
index.d.ts:
declare module '#types/express-serve-static-core' {
interface Request {
user?: User
}
}
The important part is that the declaration merging logic for this is part of #types/express-serve-static-core (see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/express-serve-static-core/index.d.ts).
After loosing a few brain cells I provide myself with answer.
To fix the issue I had to recreate folder structure that can be found in this issue
https://github.com/microsoft/TypeScript/issues/39581
Then I modified tsconfig.json so its typeRoots would look like this
"typeRoots": [
"src/typings/",
"node_modules/#types/"
]
Then, to augment Request type with my custom types I had to use import expression, so in the end the index.d.ts file would have this inside
declare namespace Express {
export interface Request {
user: import("mongoose").Model<import("./../../models/user").User>;
}
}
tsconfig for the guy in the comments
{
"compilerOptions": {
/* Enable incremental compilation */
"target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
"lib": [
"es6"
] /* Specify library files to be included in the compilation. */,
"allowJs": true /* Allow javascript files to be compiled. */,
/* Concatenate and emit output to single file. */
"outDir": "build" /* Redirect output structure to the directory. */,
"rootDir": "src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
/* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": [
"src/typings/",
"node_modules/#types/"
] /* List of folders to include type definitions from. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
/* Advanced Options */
"resolveJsonModule": true /* Include modules imported with '.json' extension */,
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}

Typescript non-relative import with node js and ES modules does not work

I am trying to create a simple Typescript script that uses ES modules, execute it with node and have sourcemaps for debugging.
The script executed with node test.ts:
import foo from './foo'
//import bar from './../src/util/bar' //works
//import bar from 'src/util/bar' // does not work. Linter nor tsc show any warning, but when trying to execute, error says bar can't be found
import bar from '#util/bar' // same as above
console.log('test 0')
console.log(foo + bar)
export {}
foo.ts:
export default "foo_module"
bar.ts is very similar.
Here is tsconfig.json:
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "ESNext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"allowJs": true, /* Allow javascript files to be compiled. */
"jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "lib", /* Redirect output structure to the directory. */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": ".", /* Base directory to resolve non-absolute module names. */
"paths": {
"*":[ "./../*" ],
"#util/*":[ "./../src/util/*" ],
}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
}
When all imports are relative, everything works fine.
When bar.ts is imported using non-relative import (src/util/bar), tsc and linter do not complain, but when trying to execute it with node the following error appears:
internal/modules/esm/default_resolve.js:100
let url = moduleWrapResolve(specifier, parentURL);
^
Error: Cannot find package '#util/bar' imported from <...omitted>\scripts\lib\scripts\test.js
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:100:13)
at Loader.resolve (internal/modules/esm/loader.js:72:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:156:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)
at link (internal/modules/esm/module_job.js:41:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
Minimal example for reproduction is here
Looks like Typescript does not support converting paths back to relative.
this feels like a Typescript missing feature.

Typescript with async/await, Promise and function default parameters

I have a nodeJS-Express-Typescript project where I want to use some native promises with async/await and also some default value for a function. This would be a simple example of what I can achieve:
sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, 500));
}
async someFunction(param = "default") {
doStuff(param);
await sleep(500);
doSomeMoreStuff();
}
The IDE warns me about this error:
$ tsc -p .
error TS2468: Cannot find global value 'Promise'.
spec/routes/users.spec.ts(508,23): error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor. Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.
src/utils/sleep.ts(10,20): error TS2693: 'Promise' only refers to a type, but is being used as a value here.
so I have to add es2015 as target in my tsconfig.json:
"target": "es2015"
But then, this error comes when executing the transpiled JS:
/../users-api/dist/src/repository/realm-helper.js:21
static init(development = false) {
^
SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/../users-api/dist/src/routes/users.js:4:24)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
So that I have to change the target to "es5":
"target": "es5"
Which leads to a vicious circle.
I've tried changing the value of "target" and "module" and it always fails something.
Am I missing something here? In theory, typescript 2.2 supports both features so I don't get why I can't transpile.
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"rootDir": ".",
"sourceMap": true,
"module": "commonjs",
"target": "es5"
},
"include": [
"./src/**/*",
"./spec/**/*"
]
}
typescript 2.4.1
node 4.4.7
Try Adding lib section with es2015.promise in tsconfig.json
"lib": [
"dom",
"es5",
"scripthost",
"es2015.promise"
],
You can see the full sample here: https://github.com/basarat/typescript-book/tree/master/code/async-await/es5

Resources