/bin/sh: tsc: command not found - linux

I am using the Visual Studio Code to work with TypeScript.
I created the tasks.json file in order to compile the ts files, but I getting this error when I run it:
/bin/sh: tsc: command not found
The terminal process terminated with exit code: 127
This is my tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
]
}
]
}
Does anyone know what is this?
I am using Linux Mint.
It is not duplicated one of tsc is not recognized as internal or external command
I already tried those steps but it did not worked.
This is my tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true
}
}
Using the terminal when I type node -v it is showing this:
v11.2.0
tsc -v:
Version 3.1.6

It appears that the binary tsc is not in your path. Have the package(s) for TypeScript been installed? This is probably a duplicate of tsc is not recognized as internal or external command

Related

tsc don't transpile .at() function

When transpiling TS to JS compatible with NodeJS v14, using the following config:
{
"compilerOptions": {
"lib": ["es2020"],
"rootDir": "src",
"outDir": "build",
"module": "commonjs",
"moduleResolution": "node",
"target": "es2020",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
}
}
It was expected that the usage of at(index) would be converted to a compatible JS code, but in fact, when I ran the built code it generates (...).at is not a function error.
[1,2,3].at(-1)
The transpiled code still makes use of .at(index), but I was expecting it would get transpiled to something compatible with the target set on tsconfig.js
target: "es2020"
What am I not getting correctly here?
The TypeScript compiler considers this method as syntactically correct, which is why it does not downlevel it. You can read it better explained in this great answer to a similar question.
However I did not mark your question as duplicate because there is one information that's missing: why don't you have a compiler error?!
If you go to the TS playground, you can see that there is an immediate error if you try to write:
[1, 2, 3].at(0);
Property 'at' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.
My guess is that you don't have an error because you are using a wrong version of the package #types/node (most probably the latest one). Indeed, as indicated on node.green, Array.prototype.at() is supported since NodeJS 16.8.0. As such, the latest typings for NodeJS reflect that fact and they provide a signature for that method, which gets automatically included by TSC.
In conclusion, install the same version of NodeJS types as your NodeJS runtime and you should get the TSC error:
npm install #types/node#14

How to use TypeScript Declaration File with mocha tests?

I have a TypeScript project, where the test are written in TypeScript. I want to use mocha to directly test TS files. I'm using ts-node for that, as described in ts-node#mocha.
In the project I'm using a JS library which doesn't have TS type definitions. So I created a d.ts file for that. Everything works well when compiling and running the projects. However mocha fails with:
% yarn mocha --require ts-node/register --require source-map-support/register ./test/*.ts
src/client.ts:3:26 - error TS7016: Could not find a declaration file for module 'algosdk'. '/home/robert/projects/algorand/ts-mocha/node_modules/algosdk/index.js' implicitly has an 'any' type.
Try `npm install #types/algosdk` if it exists or add a new declaration (.d.ts) file containing `declare module 'algosdk';`
3 import * as algosdk from "algosdk";
It seams that ts-node doesn't recognize the d.ts files.
The only solution which works is to use require instead of import statement, but this will not link the types to algosdk.
How to use mocha with TS files and type definition file?
This is the project structure (also in github):
├── build <-- JS from compiled TS go here
├── node_modules
├── package.json
├── src
├── #types <-- d.ts files
├── test
├── tsconfig.json
The tsconfig.json:
{
"compilerOptions": {
"target": "es2017",
"lib": ["esnext"],
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"outDir": "./build",
"typeRoots" : ["./#types", "node_modules/#types"]
},
"exclude": ["**/node_modules"],
"include": [
"src",
"test",
"#types"
]
}
UPDATE
I confirm that the problem is related to ts-node, not to mocha. Running:
yarn ts-node src/client.ts
returns the same error.
References:
Mocha + ts-node
TypeScript: Could not find a declaration file for module in unit tests, only
you can add the following configuration to tsconfig.json file.
"ts-node": {
"files": true
},
it works well on my side.

Intellisense for module-alias Package

I am using this npm module (module-alias)
https://www.npmjs.com/package/module-alias in my Node project.
To make use of this package you have to set path aliases in the package.json file.
However, using this package comes with the disadvantage, that intellisense doesn't work anymore.
My question is how to enable intellisense with those path aliases?
The problem is that you did not register those aliases anywhere with your linter. I would gerenally suggest to use ESLint here (even if you use TypeScript as TSLint will be discontinued in favour of ESLint). My examples will include the TypeScript endings as well. If you definately want to make it work for JavaScript only you can skip the .ts .tsx extentensions in the eslint) So to make intellisense work do this in
.eslintrc.js
settings: {
"import/resolver": {
alias : {
map: [
["#","./src"]
],
extensions: [".js", ".jsx", ".ts", ".tsx"],
},
}
},
Note that in this case you will need the import Plugin for ESLint. If you don't already have it install it.
If you are using TypeScript you will also have to make that alias known to your compiler. So add this to your
tsconfig.json
"compilerOptions": {
"baseUrl": ".",
"paths": [
"#/*" : [
"src/*"
]
]
}

How to force tsc to ignore node_modules folder?

I'm using tsc build tasks. Unfortunately I'm always getting the same errors from the node modules folder
Executing task: .\node_modules\.bin\tsc.cmd --watch -p .\tsconfig.json <
node_modules/#types/node/index.d.ts(6208,55): error TS2304: Cannot find name 'Map'.
node_modules/#types/node/index.d.ts(6215,55): error TS2304: Cannot find name 'Set'.
node_modules/#types/node/index.d.ts(6219,64): error TS2304: Cannot find name 'Symbol'.
node_modules/#types/node/index.d.ts(6225,59): error TS2304: Cannot find name 'WeakMap'.
node_modules/#types/node/index.d.ts(6226,59): error TS2304: Cannot find name 'WeakSet'.
10:13:18 - Compilation complete. Watching for file changes.
I already added the directory to the ignore at tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"strict": false,
"noImplicitAny": false,
"strictPropertyInitialization": false,
"esModuleInterop": true,
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
"./node_modules",
"./node_modules/*",
"./node_modules/#types/node/index.d.ts",
]
}
What I'm doing wrong? What should I do in order to ignore those errors?
I'm using VsCode and tsc Version 2.9.2
Quickfix is to skip the check
{
"compilerOptions": {
"skipLibCheck": true
},
}
Add an empty "types" option in "compilerOptions":
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"strict": false,
"noImplicitAny": false,
"strictPropertyInitialization": false,
"esModuleInterop": true,
"types": []
},
"include": [
"src/*"
],
"exclude": [
"node_modules",
"./node_modules",
"./node_modules/*",
"./node_modules/#types/node/index.d.ts",
]
}
From https://www.typescriptlang.org/docs/handbook/tsconfig-json.html
#types, typeRoots and types
By default all visible “#types” packages are included in your
compilation. Packages in node_modules/#types of any enclosing folder
are considered visible; specifically, that means packages within
./node_modules/#types/, ../node_modules/#types/,
../../node_modules/#types/, and so on.
...
Specify "types": [] to disable automatic inclusion of #types packages.
Keep in mind that automatic inclusion is only important if you’re
using files with global declarations (as opposed to files declared as
modules). If you use an import "foo" statement, for instance,
TypeScript may still look through node_modules & node_modules/#types
folders to find the foo package
You can do this right on the command line
tsc --skipLibCheck
I met this issue with typescript#3.2.1 and fixed by upgrading it to 3.7.3.
Notice with typescript#3.2.1, the skipLibCheck does not take effect. When I upgraded TypeScript the skipLibCheck: true works.
set "skipLibCheck": true inside tsconfig.json
I had a similar issue in a monorepo using yarn workspaces.
Turned out my app was using a different TypeScript version to the root workspace and bringing those in sync fixed the issue.
You can verify this in your own repo by running yarn list typescript or npm ls typescript.
If you have multiple versions of TypeScript then upgrade your projects with the lower version so that all have the highest version currently in use in your repo, e.g. yarn add -D typescript#4.5.5 (or whatever version you need)
This is random but none of the solutions here worked. I had an error occuring in a node_module and what fixed it was this:
Changing:
target: "esnext"
To any specific version, e.g.:
target: "es2020"
In case it helps some other poor traveller 🤷‍♂️
If you find yourself here and none of the other answers is solving the problem, check to make sure that you haven't set maxNodeModuleJsDepth.
If you have allowJs enabled this option lets TypeScript attempt to infer types from modules in node_modules, and skipLibCheck doesn't have any effect on that, because it's reading javascript files and not type declarations.
The default setting for maxNodeModuleJsDepth is 0, and in the vast majority of cases that's what you want (and you should prefer using #types packages instead of turning this on).
if you are here and none of the following has worked for you:
❌ Updgrade/downgrading typescript version
(NOTE: When you run the tsc command, the globally installed typescript version is used as the compiler. So even if you have the latest typescript version in your package.json, you will have to upgrade typescript globally. With npm thats npm install typescript#latest -g )
❌ Adding/Editing tsconfig options: target, types, include, exclude, allowJs, skipLibCheck
❌ npm update
❌ Deleting node_modules && npm i
❌ Deleting package-lock (don't do that btw) && npm i
I want to leave this link from the typescript config docs here for you:
What does this("types") affect?
With this knowledge, this solved the issue for me:
See what module is causing the error in the log when you run tsc. (For me it was node_modules/mongoose/types/*, so mongoose was the culprit.)
Convert all your ES6 imports of this module to commonjs's require(). (In my case import mongoose from 'mongoose' --> const mongoose = require('mongoose') )
I hope this solves your issue
Upgrade your typescript & ts-node:
"typescript": "4.9.4"
"ts-node": "10.9.1"
and use the flag --skipLibCheck when you build or put it in the compilerOtions on the tsconfig.json file ("skipLibCheck": true)...

How do I use TypeScript 1.6 with Visual Studio Code to get generators support?

I've been targeting ES6 for a while in Visual Studio Code, but when I try to switch to TypeScript, it throws errors such as:
Generators are only available when targeting ECMAScript 6
But my tsconfig.json does have the ES6 target:
{
"compilerOptions": {
"target": "ES6",
"module": "amd",
"sourceMap": true
}
}
So I tried npm install -g typescript#1.6.0-beta but it looks like VSCode doesn't care.
Generators are not currently supported.
How can I get TypeScript and generators to work properly together in VS Code?
Update
Changing typescript.tsdk to the 1.6 binary seems to fix IntelliSense errors, but this tasks.json still prints out error TS1220: Generators are only available when targeting ECMAScript 6 or higher.:
"version": "0.1.0",
"command": "/usr/local/lib/node_modules/typescript/bin/tsc",
"showOutput": "silent",
"windows": {"command": "tsc.exe"},
"isShellCommand": true,
"args": ["app.ts"],
"problemMatcher": "$tsc"
However, /usr/local/lib/node_modules/typescript/bin/tsc --target ES6 app.ts used manually in the terminal does work.
I know now!
1. IntelliSense
You can use the typescript.tsdk setting to point VSCode to TypeScript binaries. Upgrade your TypeScript to 1.6 and set the location properly.
You can do it either in your user/workspace settings, or per project in the .vscode/settings.json file. OS X example:
"typescript.tsdk": "/usr/local/lib/node_modules/typescript/lib"
2. Compiler
You also need to make sure your .vscode/tasks.json points to the new binary and makes the compiler operate in Explicit project mode, i.e. use tsconfig.json instead of taking a list of files to compile as an argument.
{
"version": "0.1.0",
"command": "/usr/local/lib/node_modules/typescript/bin/tsc",
"showOutput": "silent",
"windows": {"command": "tsc.exe"},
"isShellCommand": true,
"args": [], //do not pass any files to the compiler. This way it will use tsconfig.json where you can set target: "ES6"
"problemMatcher": "$tsc"
}
And finally tsconfig.json (in the project's root directory):
{
"compilerOptions": {
"target": "ES6", //The key, of course.
"module": "amd",
"sourceMap": true
},
"exclude": [
"node_modules",
".vscode"
]
}
Restart the editor afterwards!
You can change your user settings in VS Code and set "typescript.tsdk" to a custom location .
If you install the nightly (npm install -g typescript#next), you can point to that version of TypeScript's lib folder.
More
Reasons and setup instructions for using ts latest are covered here : https://basarat.gitbooks.io/typescript/content/docs/getting-started.html#typescript-version

Resources