mirror of
https://github.com/jbranchaud/til
synced 2026-03-04 15:08:45 +00:00
Compare commits
3 Commits
4840461afa
...
7da6d33f9d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7da6d33f9d | ||
|
|
72e3d551f3 | ||
|
|
6f99af3ec5 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1312 TILs and counting..._
|
||||
_1314 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -470,6 +470,7 @@ _1312 TILs and counting..._
|
||||
- [String Interpolation With Template Literals](javascript/string-interpolation-with-template-literals.md)
|
||||
- [Support Nested Matching In Custom Jest Matchers](javascript/support-nested-matching-in-custom-jest-matchers.md)
|
||||
- [Tell Jest To Focus On Running Only One Test](javascript/tell-jest-to-focus-on-running-only-one-test.md)
|
||||
- [Tell Node To Treat JS Files As ESM](javascript/tell-node-to-treat-js-files-as-esm.md)
|
||||
- [Tell Prettier To Not Format A Statement](javascript/tell-prettier-to-not-format-a-statement.md)
|
||||
- [Test Coverage Stats With Jest](javascript/test-coverage-stats-with-jest.md)
|
||||
- [Test Timing-Based Code With Jest Fake Timers](javascript/test-timing-based-code-with-jest-fake-timers.md)
|
||||
@@ -1211,6 +1212,7 @@ _1312 TILs and counting..._
|
||||
- [Create Union Type From Constants](typescript/create-union-type-from-constants.md)
|
||||
- [Extract Object Type Keys Into A Union Type](typescript/extract-object-type-keys-into-a-union-type.md)
|
||||
- [Extract Object Type Values Into A Union Type](typescript/extract-object-type-values-into-a-union-type.md)
|
||||
- [Generate An Initial tsconfig File](typescript/generate-an-initial-tsconfig-file.md)
|
||||
- [Generate Inferred Type From Zod Schema](typescript/generate-inferred-type-from-zod-schema.md)
|
||||
- [Get The Return Type Of An Async Function](typescript/get-the-return-type-of-an-async-function.md)
|
||||
- [Interfaces With The Same Name Are Merged](typescript/interfaces-with-the-same-name-are-merged.md)
|
||||
|
||||
24
javascript/tell-node-to-treat-js-files-as-esm.md
Normal file
24
javascript/tell-node-to-treat-js-files-as-esm.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Tell Node To Treat JS Files As ESM
|
||||
|
||||
By default, Node will treat all `.js` files as CommonJS. That means you'll need
|
||||
to use the CommonJS export and require syntax to share code between files. If
|
||||
you'd instead like to opt-in to ESM (ECMAScript Modules), you'll need to update
|
||||
your `package.json`.
|
||||
|
||||
Add the following line:
|
||||
|
||||
```json
|
||||
"type": "module",
|
||||
```
|
||||
|
||||
This will tell node that instead of CJS, it should treat all `.js` files as
|
||||
ESM. This means that ESM-based `export` and `import` syntax will work.
|
||||
|
||||
This also means you don't need to be defining your files with the `.mjs`
|
||||
extension.
|
||||
|
||||
For more extensive reading on this, see:
|
||||
|
||||
- [ECMAScript Modules in Node.js](https://www.typescriptlang.org/docs/handbook/esm-node.html)
|
||||
- [Getting Started with (and Surviving) Node.js ESM](https://formidable.com/blog/2021/node-esm-and-exports/)
|
||||
- [`.mts` is a cool file extension (TypeScript ES modules)](https://mtsknn.fi/blog/mts-file-extension/)
|
||||
51
typescript/generate-an-initial-tsconfig-file.md
Normal file
51
typescript/generate-an-initial-tsconfig-file.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# Generate An Initial tsconfig File
|
||||
|
||||
A new `tsconfig.json` file can be generated using [the `tsc`
|
||||
CLI](https://www.typescriptlang.org/docs/handbook/compiler-options.html) which
|
||||
is part of the `typescript` node package.
|
||||
|
||||
You'll first want to add `typescript` to your project:
|
||||
|
||||
```bash
|
||||
$ npm install typescript --save-dev
|
||||
```
|
||||
|
||||
Since it is a local project dependency, you'll want to add `tsc` as a script in
|
||||
your `package.json`.
|
||||
|
||||
```json
|
||||
"scripts": {
|
||||
"tsc": "tsc"
|
||||
}
|
||||
```
|
||||
|
||||
Now you can use `npm` to run `tsc --init` like so:
|
||||
|
||||
```bash
|
||||
$ npm run tsc -- --init
|
||||
```
|
||||
|
||||
Notice the delimiting `--` which tells `npm` to pass the remaining arguments to
|
||||
the command being invoked. This makes sure `--init` gets passed as an argument
|
||||
to `tsc`.
|
||||
|
||||
This will generate a huge, mostly commented-out `tsconfig.json` file full of
|
||||
annotations that looks something like this:
|
||||
|
||||
```json
|
||||
{
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig to read more about this file */
|
||||
|
||||
/* Projects */
|
||||
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
||||
/* ... */
|
||||
|
||||
/* Language and Environment */
|
||||
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||
/* ... */
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
[source](https://stackoverflow.com/a/57510415/535590)
|
||||
Reference in New Issue
Block a user