Typescript: Targeting ES20XX results in errors when building

Created on 2 Jan 2020  ·  5Comments  ·  Source: microsoft/TypeScript

Disclaimer:
I'm aware that installing the dependencies tsc is complaining about will probably fix the issues. However nothing like that is mentioned in the docs (as far as I'm aware) and in my opinion (optional) external dependencies should be mentioned somewhere.


Search Terms:
target es2016 es2017 es2018 es2019 babel types error module not found

Code
tsconfig.json

{
  "compilerOptions": {
    "outDir": "dist",
    "target": "ES2019"
  },
  "include": [ "src" ]
}

package.json

{
  "name": "my-package",
  "private": true,
  "version": "1.0.1",
  "description": "",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "tsc",
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepare": "npm run build"
  },
  "license": "MIT",
  "devDependencies": {
    "@types/jest": "^24.0.25",
    "@types/node": "^13.1.2",
    "jest": "^24.9.0",
    "rimraf": "^3.0.0",
    "ts-jest": "^24.2.0",
    "typescript": "^3.7.4"
  }
}

Expected behavior:
npx tsc compiles src to code matching ES2019 for use with Node 12.4.0 LTS.

Actual behavior:
The build fails with following error:

> [email protected] prebuild /home/user/development/my-monorepo/packages/my-package
> rimraf dist


> [email protected] build /home/user/development/my-monorepo/packages/my-package
> tsc

node_modules/@types/babel__core/index.d.ts:13:20 - error TS2307: Cannot find module '@babel/types'.

13 import * as t from "@babel/types";
                      ~~~~~~~~~~~~~~

node_modules/@types/babel__core/index.d.ts:14:31 - error TS2307: Cannot find module '@babel/parser'.

14 import { ParserOptions } from "@babel/parser";
                                 ~~~~~~~~~~~~~~~

node_modules/@types/babel__generator/index.d.ts:11:20 - error TS2307: Cannot find module '@babel/types'.

11 import * as t from '@babel/types';
                      ~~~~~~~~~~~~~~

node_modules/@types/babel__template/index.d.ts:9:31 - error TS2307: Cannot find module '@babel/parser'.

9 import { ParserOptions } from "@babel/parser";
                                ~~~~~~~~~~~~~~~

node_modules/@types/babel__template/index.d.ts:10:54 - error TS2307: Cannot find module '@babel/types'.

10 import { Expression, File, Program, Statement } from "@babel/types";
                                                        ~~~~~~~~~~~~~~

node_modules/@types/babel__traverse/index.d.ts:11:20 - error TS2307: Cannot find module '@babel/types'.

11 import * as t from "@babel/types";
                      ~~~~~~~~~~~~~~

node_modules/@types/babel__traverse/index.d.ts:31:5 - error TS2411: Property 'scope' of type 'Scope' is not assignable to string index type '(VisitNodeFunction<S, any> & VisitNodeFunction<S, any>) | (VisitNodeFunction<S, any> & VisitNodeObject<S, any>) | (VisitNodeObject<...> & VisitNodeFunction<...>) | (VisitNodeObject<...> & VisitNodeObject<...>)'.

31     scope?: Scope;
       ~~~~~

node_modules/@types/babel__traverse/index.d.ts:32:5 - error TS2411: Property 'noScope' of type 'boolean' is not assignable to string index type '(VisitNodeFunction<S, any> & VisitNodeFunction<S, any>) | (VisitNodeFunction<S, any> & VisitNodeObject<S, any>) | (VisitNodeObject<...> & VisitNodeFunction<...>) | (VisitNodeObject<...> & VisitNodeObject<...>)'.

32     noScope?: boolean;
       ~~~~~~~

node_modules/@types/jest/index.d.ts:477:51 - error TS2307: Cannot find module 'jest-diff'.

477             diff(a: any, b: any, options?: import("jest-diff").DiffOptions): string | null;
                                                      ~~~~~~~~~~~


Found 9 errors.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] build: `tsc`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the [email protected] 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!     /home/user/.npm/_logs/2020-01-02T17_47_57_749Z-debug.log

Playground Link:
Not applicable.

Question

Most helpful comment

You need to set moduleResolution to node. Because this is so common, I've filed https://github.com/microsoft/TypeScript/issues/35876. Please give it an upvote if that would've helped!

All 5 comments

Despite the errors all files are still being built properly.

I've successfully narrowed down the issue. In its minimal form it's created by installing both jest and typescript before executing tsc.

Reproduction repository: https://github.com/DragonRaider5/ts-jest-es20XX

Install instructions:

#!/bin/bash
git clone https://github.com/DragonRaider5/ts-jest-es20XX.git ts-jest-target-es20XX-bug
cd ts-jest-target-es20XX-bug
npm install
npx tsc

You need to set moduleResolution to node. Because this is so common, I've filed https://github.com/microsoft/TypeScript/issues/35876. Please give it an upvote if that would've helped!

Indeed, this fixes it. I was unable to find anything regarding this problem, guess I'm bad at googling :rofl:

Thank you very much, I like your idea - upvoted.

@DanielRosenwasser Thanks for providing the solution!

I still, however, don't understand why simply installing jest causes this compile error when no files within tsconfig.json > include reference it. Can you explain why this is?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CyrusNajmabadi picture CyrusNajmabadi  ·  3Comments

kyasbal-1994 picture kyasbal-1994  ·  3Comments

DanielRosenwasser picture DanielRosenwasser  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

dlaberge picture dlaberge  ·  3Comments