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:

yarn global add @tsmodule/tsmodule

Create a new project:

tsmodule create [—-react] project-name

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

tsmodule convert

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

Using create-esnext

Install create-esnext globally:

yarn global add create-esnext

# or

npm i -g create-esnext

In the folder where you would run npm init:

yarn create esnext

# or 

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