Is there a way to "npm init" an ES module?
Asked Answered
R

5

15

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)?

Rodrigorodrigue answered 16/7, 2020 at 8:6 Comment(6)
With npm you can customize the questions asked and fields created during the npm init process, as described in the docs.Whaling
type is not a standard field in package.json. docs.npmjs.com/files/package.jsonHindward
@Whaling I am generating project directories with yarn, so just setting my own local preference will not work. Worst case scenario, I can read package.json, modify it, and write it back. The question is whether I can do this easily, in the shell, using npm or yarn, in a way that does not involve just reading package.json and handling it myself.Rodrigorodrigue
@Hindward Support was added for the type field in 2019.. The question was not "Should I put a type field in my package.json?"Rodrigorodrigue
@Christian - Yes, the type field is currently experimental and who knows it may just be temporary - hence why npm hasn't formally listed as a "standard" field. Personally, I (like many others) don't use the experimental ESM syntax in production code.Whaling
@Whaling Thankfully, none of this is production code. That field just tells Node to use the --experimental_modules flag and process ES6 modules. Like I said, it is not a concern.Rodrigorodrigue
E
12

This does exactly what you asked:

npm init es6

It creates a package.json with the "type": "module" setting.

source: https://www.npmjs.com/package/create-es6

Excursionist answered 4/4, 2022 at 3:23 Comment(1)
This annoyingly sets "license": "AGPL-version-3.f0",Pteranodon
C
10

Saw this question a while ago, and for the longest time I used "npm init esnext". I just figured out how to edit package.json keys directly from the command line:

npm init -y; npm pkg set type="module";

Although the first command will log the content of package.json without "type": "module" to the console, if you open package.json you will see that it has been added properly by the second command.

Cloy answered 19/2, 2023 at 5:40 Comment(2)
What do you mean by "it will appear incorrect"?Pteranodon
@Pteranodon I edited the answer to clarify what the original author meant (which I figured out by running the commands myself)Lillian
C
1

While @stackoverflow221's answer is good, it adds the type at the end of the package.json, which feels slightly unnatural to me.

I prefer to put the type right above the entry, given they are closely related.

For that, I have the following bash function on my .bashrc, but it should be easy to adapt to any shell configuration:

# custom 'npm init'
# - runs regular 'npm init', interactively
# - then, if successful, add "type": "module" to the generated package.json,
# - right above the line containing "main".
function npm-init () {
  npm init 

  # if package.json was not created, exit (this means the user cancelled)
  # (npm init has no error codes)
  if [[ ! -f package.json ]]; then
    return 1
  fi

  sed -i '/"main":/i\  "type": "module",' package.json
}

Then, just run npm-init instead of npm init

Cabbageworm answered 20/12, 2023 at 16:14 Comment(0)
G
-1

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.

Gottuard answered 16/7, 2020 at 8:21 Comment(4)
Note, if you come here seeking the same ask as in OP, i.e., any flags for npm init or yarn init that will add that "type": "module" property in package.json, then this is not the answer. It just adds esm into dependencies, which is not what I wanted. YMMW.Oilskin
Now it's 2022 and this answer may very well be the best possible answer, but it's still horrible. Node.js and all the other laggards really need to get their act together and kick commonjs/require to the curb. This pain has been going on for way too many years.Scrape
This apparently does not add "type": "module" to package.json. See the selected answer instead.Rodrigorodrigue
25 years ago perl was dominant and most of the pain came from the garbage module system and the fact that types were a bolt-on. Some things never changeApologue
M
-1

You could set default values when running pnpm init

pnpm config set init.type module
Machellemachete answered 25/4, 2022 at 12:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.