You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+28-3Lines changed: 28 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,8 @@ Minimalistic example of configuring TypeScript and Node to:
7
7
* import your own modules without specifying an extension
8
8
* run the resulting JavaScript code
9
9
10
+
There is no need for Babel.
11
+
10
12
# Emit ES modules code
11
13
12
14
In `tsconfig.json`, set this in `compilerOptions`:
@@ -16,23 +18,42 @@ In `tsconfig.json`, set this in `compilerOptions`:
16
18
"module": "esnext", // Output `import`/`export` ES modules
17
19
```
18
20
21
+
19
22
# Import modules that use Node built-ins (`http`, `url` etc.)
20
23
21
24
* run `npm install --save-dev @types/node`
22
25
* in `tsconfig.json` under `compilerOptions`, set
23
26
*`"moduleResolution": "node"`, so `tsc` can find modules [when targeting ES6+](https://github.com/Microsoft/TypeScript/issues/8189)
24
27
*`"types": ["node"]` to avoid errors related to Node built-in modules
25
28
29
+
26
30
# Import modules that don't have named exports
27
31
28
-
Set `"allowSyntheticDefaultImports": true` in `tsconfig.json`. In our code,
29
-
instead of `import { InfluxDB } from 'influx` we have to write:
32
+
Normally we could write in TypeScript
33
+
34
+
import { InfluxDB } from 'influx';
35
+
36
+
but when generating ES modules code, that statement will be passed through as is, and will cause Node to fail with
37
+
38
+
> SyntaxError: The requested module 'influx' does not provide an export named 'InfluxDB'
39
+
40
+
because [`node-influx` doesn't provide named exports](https://github.com/node-influx/node-influx/issues/298) (and neither does an even more popular module, [`apollo-server`](https://github.com/apollographql/apollo-server/issues/1356#issuecomment-565277759)).
41
+
42
+
One alternative would be to generate old ugly commonjs modules code by,
43
+
44
+
* removing the `"type": "module"` line from `package.json`, and
45
+
* changing the module line to `"module": "CommonJS"` in `tsconfig.json` (`allowSyntheticDefaultImports` also becomes unnecessary)
46
+
47
+
Another is to import the entire module:
30
48
31
49
```js
32
50
importInfluxfrom'influx';
33
51
constinflux=newInflux.InfluxDB();
34
52
```
35
53
54
+
However, this will generate `Error TS1192: Module '...' has no default export.` To prevent that, set `"allowSyntheticDefaultImports": true` in `tsconfig.json`.
55
+
56
+
36
57
# Import your own modules without specifying an extension
37
58
38
59
When transpiling, [TypeScript won't generate an extension for you](https://github.com/microsoft/TypeScript/issues/16577). Run Node with the `node --experimental-specifier-resolution=node` parameter:
@@ -41,12 +62,16 @@ When transpiling, [TypeScript won't generate an extension for you](https://githu
41
62
42
63
Otherwise, [node mandates that you specify the extension](https://nodejs.org/api/esm.html#esm_mandatory_file_extensions) in the `import` statement.
43
64
65
+
To support optional chaining, add the `--harmony` flag to the node command line.
66
+
67
+
44
68
# Run the resulting JavaScript code
45
69
46
70
Add `"type": "module"` to `package.json`, because [TypeScript can't generate files with the .mjs extension](https://github.com/microsoft/TypeScript/issues/18442#issuecomment-581738714).
47
71
72
+
48
73
# ESLint
49
74
50
-
To be able to run `eslint`, we must create an `.eslintrc.cjs` file, rather than a `.js` one. Then, install the required dependencies:
75
+
To be able to run `eslint`, we must create an `.eslintrc.cjs` file, rather than a `.js` one (due to `"type": "module"` in `package.json`). Then, install the required dependencies:
51
76
52
77
npm i -D eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser
0 commit comments