javascriptnode.jsnpmes6-modulesnpm-init

Initializing ES6 module project with npm


Is there a way to have npm init directly set up an ES6 module, setting "type":"module" in package.json (or another npm command to set it)?

I always just modify package.json manually and add it, but I'm wondering if there's a way to do it directly with npm on the command line so I don't have to keep doing that. Just a convenience thing. Plus if I can do it with npm it's easier to automate.


Solution

  • npm init itself doesn’t have a flag to auto-set "type": "module" in package.json. By default, it creates CommonJS projects.

    So, there’s no direct npm init flag for "type": "module". The simplest automated way is:

    npm init -y && npm pkg set type=module