'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)?
Solution 1:[1]
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
I personally use TSModule to create new ESM packages in lieu of npm init because it handles this, plus I can author pure ESM with TypeScript (and it standardizes the exports package.json field).
Solution 2:[2]
For ES compatible modules there exists create-esm package. Among other things (docs) it also populates module field.
So it seems you could use npm init esm.
Solution 3:[3]
You could set default values when running pnpm init
npm config set init.type module
Solution 4:[4]
This does exactly what you asked:
npm init es6
It creates a package.json with the "type": "module" setting.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | C. Lewis |
| Solution 3 | |
| Solution 4 | Arthur |
