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 😎
.
├── 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
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.
module: "esnext"
: Set the module code generation toesnext
, see inherited tsconfig.settings.json
- Install Node.js 12 or type
nvm use
if you use nvm - Install project dependencies with:
npm install
- Start app with:
npm run start
- Browse http://localhost:3000
You should see the following JSON response:
{
"messages": ["Hello from ES Module dependency", "Hello from local ES Module"]
}
MIT © kevinpollet