1

I've got a very simple, one-file TypeScript project. I'm trying to produce ESM (ES6 module) output for that project.

When I have TypeScript configured to produce ESM output, along the lines of this ...

"compilerOptions": {
   "module": "ES2015",
   "target": "ES2015",
}

... then I cannot have Jest installed in my project (!?) or I get these errors related to Babel (a transitive dependency of Jest, not something I'm actually using at all in this project):

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:9:20 - error TS2307: Cannot find module '@babel/types'.

9 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:10:20 - error TS2307: Cannot find module '@babel/types'.

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

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

30     scope?: Scope;
       ~~~~~

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

31     noScope?: boolean;
       ~~~~~~~


Found 8 errors.

I've tried restricting the TypeScript compiler to only my own code ...

"files": [
   "index.ts",
]

I've tried various explicit values for typeRoots, to try and shut this error up ...

"compilerOptions": {
   "module": "ES2015",
   "target": "ES2015",
   "outDir": "modules",
   "typeRoots" : ["node_modules/@types", "node_modules/@babel/types"],
}

Nothing I try works. How can I use "module": "ES2015", to avoid transpiling import statements, without uninstalling Jest or Babel from this project?

Edit: I forgot, here's the full project; it's one TypeScript file, plus a couple configuration files.

ELLIOTTCABLE
  • 17,185
  • 12
  • 62
  • 78
  • Which modules is your one file importing? It seems that something has dev dependency on `@babel/types`, is it installed in `node_modules/@types/babel__types`? – artem Aug 17 '19 at 19:46
  • The folder `node_modules/@babel/types` exists; but the folder `node_modules/@types/babel__types` does not. Should it? A clean `npm install` doesn't produce that folder. – ELLIOTTCABLE Aug 17 '19 at 21:01
  • No, that was wrong guess on my part, [@babel/types](https://github.com/babel/babel/blob/master/packages/babel-types/package.json) comes with typescript typings already within. So I cloned your repo, reproduced your error, did `npm i typescript` to get at least typescript installed, then run `tsc --traceResolution -p tsconfig.json > module-resolution.log`, and the first thing at that log was `Module resolution kind is not specified, using 'Classic'.` – artem Aug 18 '19 at 01:11
  • 'Classic' is the default when module is not "commonjs", probably it's for backward compatibility with code written very long time ago. So with your "module" and "target", you also have to set `"moduleResolution": "node"` in `tsconfig.json`, then typescript will find what it needs to find in `node_modules`. – artem Aug 18 '19 at 01:13
  • There's [github issue](https://github.com/microsoft/TypeScript/issues/32432) proposing to change the default, rejected out of fear of breaking existing code. – artem Aug 18 '19 at 01:29
  • That 100% nailed it! Drop this into an actual answer, so I can give you the acceptance points? (= – ELLIOTTCABLE Aug 19 '19 at 06:07

0 Answers0