https://github.com/kevinpollet/typescript-es-modules-node-example
ES Modules written in TypeScript running on Node.js
https://github.com/kevinpollet/typescript-es-modules-node-example
es-modules esnext example nodejs typescript
Last synced: 4 months ago
JSON representation
ES Modules written in TypeScript running on Node.js
- Host: GitHub
- URL: https://github.com/kevinpollet/typescript-es-modules-node-example
- Owner: kevinpollet
- License: mit
- Created: 2019-04-24T07:16:32.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-09T00:42:01.000Z (almost 3 years ago)
- Last Synced: 2025-04-04T04:04:05.888Z (6 months ago)
- Topics: es-modules, esnext, example, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 1.28 MB
- Stars: 24
- Watchers: 1
- Forks: 1
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Running TypeScript ES Modules with Node.js
[](https://github.com/kevinpollet/typescript-es-modules-node-example/actions)
[](./LICENSE.md)Based on the [Announcing a new experimental modules](https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff) post from the Node team, this repository is an example of a TypeScript app running with the new `experimental modules` feature shipped with Node.js 12. As explained in the linked post, it's now possible to use `import` and `export` syntax in `.js` files, so ES Modules transpiled from TypeScript can be used out of the box 😎
## Project tree
```
.
├── lerna.json
├── package-lock.json
├── package.json
├── packages
│  ├── app //--> TypeScript app transpiled as an ES Module, using es-module dependency
│  │  ├── package-lock.json
│  │  ├── package.json
│  │  └── tsconfig.json
│  └── es-module //--> ES Module written in TypeScript
│  ├── package-lock.json
│  ├── package.json
│  └── tsconfig.json
└── tsconfig.settings.json
```## Config
### Node.js
- `type: module`: New package.json field to treat all `.js` files in project as ES Modules, see [app](./packages/app/package.json) and [es-module](./packages/es-module/package.json) files.
- `--experimental-modules`: Flag to enable new experimental modules feature.
- `--es-module-specifier-resolution=node`: By default, file extensions are mandatory in import. This flag enable CommonJS-style automatic extension resolution behavior. This flag is required because [TypeScript does not add imported file extension in transpiled code](https://github.com/microsoft/TypeScript/issues/16577).### TypeScript
- `module: "esnext"`: Set the module code generation to `esnext`, see inherited [tsconfig.settings.json](./tsconfig.settings.json)
## Run app
1. Install [Node.js 12](https://nodejs.org/en/blog/release/v12.0.0/) or type `nvm use` if you use [nvm](https://github.com/creationix/nvm)
2. Install project dependencies with: `npm install`
3. Start app with: `npm run start`
4. Browse http://localhost:3000You should see the following JSON response:
```json
{
"messages": ["Hello from ES Module dependency", "Hello from local ES Module"]
}
```## License
[MIT](./License.md) © kevinpollet