Ora library doesn't compile with typescript - node.js

I am trying to build a CLI with nodejs and i tried to add some spinners in the command line using Ora. Unfortunately when i run tsc to build my JS 26 errors are thrown :
........
node_modules/ora/index.d.ts:269:9 - error TS1005: ';' expected.
269 frame(): string;
~
node_modules/ora/index.d.ts:270:1 - error TS1128: Declaration or statement expected.
270 }
~
Found 26 errors.
this my tsconfig file :
{
"compilerOptions": {
"target": "es5",
"lib": ["es2017", "es2015", "dom", "es6"],
"module": "commonjs",
"outDir": "./bin",
"sourceMap": false,
"strict": true,
"moduleResolution": "node"
},
"include": ["src/**.ts"],
"exclude": ["node_modules"]
}
I tried importing the module in multiple ways but it always seems to fail the build. thank you.

Related

Prevent tsc type checking projects in node_modules folder?

I am running into an issue whereby tsc is insisting on type-checking files in the node_modules folder, resulting in errors such as:
> my-project#0.0.0 build:ts
> tsc --project tsconfig.json
node_modules/mongoose/types/query.d.ts:619:34 - error TS1144: '{' or ';' expected.
619 toConstructor(): typeof Query<ResultType, DocType, THelpers, RawDocType>;
~
node_modules/mongoose/types/query.d.ts:619:45 - error TS1005: '>' expected.
619 toConstructor(): typeof Query<ResultType, DocType, THelpers, RawDocType>;
~
node_modules/mongoose/types/query.d.ts:619:77 - error TS1109: Expression expected.
619 toConstructor(): typeof Query<ResultType, DocType, THelpers, RawDocType>;
~
node_modules/mongoose/types/query.d.ts:622:19 - error TS1109: Expression expected.
622 update(filter?: FilterQuery<DocType>, update?: UpdateQuery<DocType> | UpdateWithAggregationPipeline, options?: QueryOptions<DocType> | null, callback?: Callback<UpdateWriteOpResult>): QueryWithHelpers<UpdateWriteOpResult, DocType, THelpers, RawDocType>;
This has only just started happening and I am trying to work out the cause and the resolution. The skipLibCheck parameter doesn't seem to be having any impact.
My tsconfig.json is as follows:
{
"compilerOptions": {
"lib": [
"es2020"
],
"module": "commonjs",
"experimentalDecorators": true,
"moduleResolution": "node",
"sourceMap": true,
"strict": false,
"target": "es2020",
"noImplicitAny": false,
"suppressImplicitAnyIndexErrors": true,
"typeRoots": [
"node_modules/#types"
],
"outDir": "dist",
"esModuleInterop": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*.ts"
],
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
},
"ts-node": {
"files": true,
}
}
Version info:
tsc 4.5.5 (provided by typescript#4.5.5)
node v16.10.0
The issue turned out to be a Typescript version mismatch, where we were using an older version than one of our dependencies.
The project was using Typescript 4.5.5 and one of the dependencies, in this case mongoose, changed to using Typescript 4.8.x in their 6.7.0 release. I didn't catch this until I checked the node_modules/mongoose/package.json and noticed that package.json specified:
"mongoose": "^6.1.2"
This meant the minor version was free to change, since it was an equivalent version, resulting in a silent breaking change appearing in our project.
This means I have two solutions:
Upgrade the Typescript version, used by the project
Use the '~' for the mongoose version to stay in 6.1.x
The first approach is what makes sense medium to long term, but the second one will get us unblocked the fastest. This is partly because upgrading the TS version will present the risk of unknowns, which we aren't ready for.

How to use get-port package from a VS Code extension?

How can I use the get-port package from a VS Code extension? I was able to use get-port in a standalone typescript sample project here, but when I try to use the same tsconfig.json with a VS Code extension I get the following error in the developer extension host log file:
[2022-04-19 15:42:18.314] [exthost] [error] Activating extension undefined_publisher.vscode-test-getport failed due to an error:
[2022-04-19 15:42:18.314] [exthost] [error] Error [ERR_REQUIRE_ESM]: require() of ES Module /home/hakon/test/vscode/vscode-test-getport/out/extension.js from /usr/share/code/resources/app/out/vs/loader.js not supported.
Instead change the require of extension.js in /usr/share/code/resources/app/out/vs/loader.js to a dynamic import() which is available in all CommonJS modules.
at Function.<anonymous> (node:electron/js2c/asar_bundle:5:13331)
at Function.<anonymous> (/usr/share/code/resources/app/out/vs/workbench/api/node/extensionHostProcess.js:104:32156)
The code can be found in the tsconfig branch here. The above failure happens when I execute the command "View Port" which the extension contributes.
The code uses the following tsconfig.json:
{
"compilerOptions": {
"module": "es6",
"target": "es6",
"outDir": "out",
"moduleResolution": "node",
"lib": [
"ES2020"
],
"sourceMap": true,
"rootDir": "src",
"strict": true
}
}
I first tried the original tsconfig.json that comes with the yo code generated "Hello world" template (see the main branch):
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2020",
"outDir": "out",
"lib": [
"ES2020"
],
"sourceMap": true,
"rootDir": "src",
"strict": true
}
}
which fails at activation (not when executing the "Show Port" command as the first example above did):
Activating extension 'undefined_publisher.vscode-test-getport' failed: require() of ES Module /home/hakon/test/vscode/vscode-test-getport/out/extension.js from /usr/share/code/resources/app/out/vs/loader.js not supported.
Instead change the require of extension.js in /usr/share/code/resources/app/out/vs/loader.js to a dynamic import() which is available in all CommonJS modules..
Any idea how I can fix this?

TypeScript compilation error, importing non default interface with curly braces

When compiling my TypeScript project, the compiler is throwing the following error:
node_modules/#types/domutils/index.d.ts:6:10 - error TS2614: Module '"../../domhandler/lib"' has no exported member 'DomElement'. Did you mean to use 'import DomElement from "../../domhandler/lib"' instead?
The offending line is:
import { DomElement } from "domhandler";
The problem is, in the typing file it is trying to import from, the DomElement interface is a non default exported interface as follows:
export interface DomElement {
attribs?: {[s: string]: string};
children?: DomElement[];
data?: any;
name?: string;
next?: DomElement;
parent?: DomElement;
prev?: DomElement;
type?: string;
}
If I remove the curly braces it does in fact work, but that seems problematic to me:
I was under the impression that only default exports can be imported without curly braces. Why is this import required without curly braces?
This issue is occurring in type definitions in the node-modules folder as provided by DefinitelyTyped. I do not want to change a dependency file. There are no related open issues in Github, so I assume it does work. In fact it works for a colleague with an older version of Node (v8) but that doesn't seem like it should make a difference.
Versions:
Node.js - 12.14.0
List item
TypeScript 3.7.2 (also tested not working on 3.7.4)
Type definitions for domhandler 2.4 (https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/domhandler)
Type definitions for domutils 1.7 (https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/domutils)
UPDATE
Here is my tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*"
]
}
},
"include": [
"src/**/*"
]
}
Based on info from aluanhaddad at GitHub I managed to get it compiling (and working), though I don't like the solution (because it's actually turning any checking for that module off).
I have removed typings to sanitize-html (and related domhandler etc.). TSC cries that it doesn't know "sanitize-html" module, so I've added a dummy module declaration inside my src folder.
src/sanitize-html.d.ts
declare module 'sanitize-html';
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
"lib": [
"es6",
"dom"
],
"include": [
"src/**/*",
"index.ts"
],
"exclude": [
"**/*.spec.ts"
]
}
build command:
tsc

Migration from Angular 6 to Angular 7 cause error - Can't resolve 'core-js/es7/reflect'

Global Angular CLI: 7.3.8
Node v10.15.3
NPM 6.4.1
macos
I'm getting this error on npm start
ERROR in
./node_modules/#angular-devkit/build-angular/src/angular-cli-files/models/jit-polyfills.js
Module not found: Error: Can't resolve 'core-js/es7/reflect' in
'/Users/XXX/projects/XXX/node_modules/#angular-devkit/build-angular/src/angular-cli-files/models'
ERROR in ./src/polyfills.ts Module not found: Error: Can't resolve
'core-js/es7/reflect' in '/Users/XXX/projects/XXX/src'
To solve this issue I've added the following paths to compilerOptions in tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"paths": {
"core-js/es7/reflect": [
"node_modules/core-js/proposals/reflect-metadata",
],
"core-js/es6/*": ["node_modules/core-js/es"]
},
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/#types"
],
"lib": [
"es2017",
"dom"
]
}
}
After Migrated to new Angular version, 'core-js/es6' or 'core-js/es7' Will not work.
You have to simply replace import 'core-js/es/'
For ex. import 'core-js/es6/symbol' to import 'core-js/es/symbol'
This will work properly.
just remove the number at the end of 'es' in the path, like 'core-js/es/reflect'. It worked for me.
I just copy all from my oldest project src/polyfills to the new imported. That's help ;) .
Getting this error in "#angular/cli": "~10.1.5" project:
Cannot find module 'core-js/es7/reflect' or its corresponding type declarations.ts(2307)
Solution:
import * as Reflect from 'core-js/es';

Cannot find declaration file for 'autobind-decorator' in typescript

I am using Typescript and I want to import 'autobind-decorator' package inside project but ı stuck here.
I am getting this error line while compiling:
cannot find declaration file for 'autobind-decorator'. Implicitly has an 'any' type
I also tried #types/autobind-decorator npm package, but it didn't work.
Is there any option to get rid of this compile error ?
Here is my tsconfig:
{
"version": "2.1.5",
"compilerOptions": {
"module": "commonjs",
"lib": ["es2015", "es2016", "dom"],
"sourceMap": true,
"noImplicitAny": true,
"target": "es6",
"jsx": "react",
"skipLibCheck": true,
"experimentalDecorators": true
},
"include": [
"./packages/ld-web/src/**/*"
],
"exclude": [
"**/node_modules",
"**/*.d.ts"
]
}
import :
import * as autobind from "autobind-decorator";
From the type declarations here it declares and exports as module,
You need to install the #types as:
npm install #types/autobind-decorator --save-dev
and import as:
import autobind = require("autobind-decorator");
This worked, not using import at all:
const { autobind } = require('autobind-decorator');
I still had errors at runtime, so I read the doc again
https://www.npmjs.com/package/autobind-decorator
and used
const { boundMethod } = require('autobind-decorator');
that works,
I tried again using import and it failed.

Resources