Skip to content

kevinpollet/typescript-es-modules-node-example

Repository files navigation

Running TypeScript ES Modules with Node.js

Build Status License

Based on the Announcing a new experimental modules 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 and es-module 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.

TypeScript

Run app

  1. Install Node.js 12 or type nvm use if you use nvm
  2. Install project dependencies with: npm install
  3. Start app with: npm run start
  4. Browse http://localhost:3000

You should see the following JSON response:

{
  "messages": ["Hello from ES Module dependency", "Hello from local ES Module"]
}

License

MIT © kevinpollet

About

ES Modules written in TypeScript running on Node.js

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published