Skip to content
Advertisement

Is there a way to “npm init” an ES module?

All modern versions of Node need to run packages as modules is "type": "module" in package.json, but I don’t see any flags for npm init or yarn init that will add that property.

Is there a flag for either package manager or an easy way to add the value to package.json (i.e., npm package-property set type module or something similar)?

Advertisement

Answer

Using TS Module

Install globally:

JavaScript

Create a new project:

JavaScript

Or convert your project to a TS Module (ESM + TypeScript):

JavaScript

See https://github.com/tsmodule/tsmodule.

Using create-esnext

Install create-esnext globally:

JavaScript

In the folder where you would run npm init:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement