esm: replace --entry-type with --input-type · nodejs/node@96e46d3 (original) (raw)

`@@ -30,45 +30,37 @@ specifier resolution, and default behavior.

`

30

30

`` The --experimental-modules flag can be used to enable support for

``

31

31

`ECMAScript modules (ES modules).

`

32

32

``

33

``

`-

Running Node.js with an ECMAScript Module

`

``

33

`+

Once enabled, Node.js will treat the following as ES modules when passed to

`

``

34

`` +

node as the initial input, or when referenced by import statements within

``

``

35

`+

ES module code:

`

34

36

``

35

``

`-

There are a few ways to start Node.js with an ES module as its input.

`

``

37

`` +

``

36

38

``

37

``

`-

Initial entry point with an .mjs extension

`

``

39

`` +

``

``

40

`` +

package.json file contains a top-level field "type" with a value of

``

``

41

`` +

"module".

``

38

42

``

39

``

`` -

A file ending with .mjs passed to Node.js as an initial entry point will be

``

40

``

`-

loaded as an ES module.

`

``

43

`` +

``

``

44

`` +

node via STDIN, with the flag --input-type=module.

``

41

45

``

42

``


```sh

43

``

`-

node --experimental-modules my-app.mjs

`

44

``


```

45

``

-

46

``

`-

--entry-type=module flag

`

47

``

-

48

``

`` -

Files ending with .js or .mjs, or lacking any extension,

``

49

``

`` -

will be loaded as ES modules when the --entry-type=module flag is set.

``

50

``

-

51

``


```sh

52

``

`-

node --experimental-modules --entry-type=module my-app.js

`

53

``


```

``

46

`` +

Node.js will treat as CommonJS all other forms of input, such as .js files

``

``

47

`` +

where the nearest parent package.json file contains no top-level "type"

``

``

48

`` +

field, or string input without the flag --input-type. This behavior is to

``

``

49

`+

preserve backward compatibility. However, now that Node.js supports both

`

``

50

`+

CommonJS and ES modules, it is best to be explicit whenever possible. Node.js

`

``

51

`` +

will treat the following as CommonJS when passed to node as the initial input,

``

``

52

`` +

or when referenced by import statements within ES module code:

``

54

53

``

55

``

`` -

For completeness there is also --entry-type=commonjs, for explicitly running

``

56

``

`` -

a .js file as CommonJS. This is the default behavior if --entry-type is

``

57

``

`-

unspecified.

`

``

54

`` +

``

58

55

``

59

``

`` -

The --entry-type=module flag can also be used to configure Node.js to treat

``

60

``

`` -

as an ES module input sent in via --eval or --print (or -e or -p) or

``

61

``

`` -

piped to Node.js via STDIN.

``

``

56

`` +

``

``

57

`` +

package.json file contains a top-level field "type" with a value of

``

``

58

`` +

"commonjs".

``

62

59

``

63

``


```sh

64

``

`-

node --experimental-modules --entry-type=module --eval \

`

65

``

`-

"import { sep } from 'path'; console.log(sep);"

`

66

``

-

67

``

`-

echo "import { sep } from 'path'; console.log(sep);" | \

`

68

``

`-

node --experimental-modules --entry-type=module

`

69

``


```

``

60

`` +

``

``

61

`` +

node via STDIN, with the flag --input-type=commonjs.

``

70

62

``

71

``

`-

package.json "type" field

`

``

63

`+

package.json "type" field

`

72

64

``

73

65

`` Files ending with .js or .mjs, or lacking any extension,

``

74

66

`` will be loaded as ES modules when the nearest parent package.json file

``

`` @@ -97,6 +89,14 @@ If the volume root is reached and no package.json is found,

``

97

89

`` Node.js defers to the default, a package.json with no "type"

``

98

90

`field.

`

99

91

``

``

92

`` +

import statements of .js and extensionless files are treated as ES modules

``

``

93

`` +

if the nearest parent package.json contains "type": "module".

``

``

94

+

``

95


```js

``

96

`+

// my-app.js, part of the same example as above

`

``

97

`+

import './startup.js'; // Loaded as ES module because of package.json

`

``

98


```

``

99

+

100

100

`## Package Scope and File Extensions

`

101

101

``

102

102

`` A folder containing a package.json file, and all subfolders below that

``

`@@ -156,6 +156,24 @@ package scope:

`

156

156

`` extension (since both .js and .cjs files are treated as CommonJS within a

``

157

157

`` "commonjs" package scope).

``

158

158

``

``

159

`+

--input-type flag

`

``

160

+

``

161

`` +

Strings passed in as an argument to --eval or --print (or -e or -p), or

``

``

162

`` +

piped to node via STDIN, will be treated as ES modules when the

``

``

163

`` +

--input-type=module flag is set.

``

``

164

+

``

165


```sh

``

166

`+

node --experimental-modules --input-type=module --eval \

`

``

167

`+

"import { sep } from 'path'; console.log(sep);"

`

``

168

+

``

169

`+

echo "import { sep } from 'path'; console.log(sep);" | \

`

``

170

`+

node --experimental-modules --input-type=module

`

``

171


```

``

172

+

``

173

`` +

For completeness there is also --input-type=commonjs, for explicitly running

``

``

174

`` +

string input as CommonJS. This is the default behavior if --input-type is

``

``

175

`+

unspecified.

`

``

176

+

159

177

`## Package Entry Points

`

160

178

``

161

179

`` The package.json "main" field defines the entry point for a package,

``