


Import './startup.js' // Loaded as ES module because of package.json my-app.js, part of the same example as above js files are treated as ES modules if the nearest parent package.json contains "type": "module". If the volume root is reached and no package.json is found, Node.js defers to the default, a package.json with no "type" field. If the nearest parent package.json lacks a "type" field, or contains "type": "commonjs". } # In same folder as preceding package.json The nearest parent package.json is defined as the first package.json found when searching in the current folder, that folder’s parent, and so on up until the root of the volume is reached. js will be loaded as ES modules when the nearest parent package.json file contains a top-level field "type" with a value of "module". Strings passed in as an argument to -eval or -print, or piped to node via STDIN, with the flag -input-type=commonjs.įiles ending with. js when the nearest parent package.json file contains a top-level field "type" with a value of "commonjs". Node.js will treat the following as CommonJS when passed to node as the initial input, or when referenced by import statements within ES module code:įiles ending in. However, now that Node.js supports both CommonJS and ES modules, it is best to be explicit whenever possible. This behavior is to preserve backward compatibility. js files where the nearest parent package.json file contains no top-level "type" field, or string input without the flag -input-type. Node.js will treat as CommonJS all other forms of input, such as. Strings passed in as an argument to -eval or -print, or piped to node via STDIN, with the flag -input-type=module. js when the nearest parent package.json file contains a top-level field "type" with a value of "module". Node.js will treat the following as ES modules when passed to node as the initial input, or when referenced by import statements within ES module code:įiles ending in. EnablingĮxperimental support for ECMAScript modules is enabled by default. Node.js contains support for ES Modules based upon the Node.js EP for ES Modules and the ECMAScript-modules implementation.Įxpect major changes in the implementation including interoperability support, specifier resolution, and default behavior. Node.js fully supports ECMAScript modules as they are currently specified and provides limited interoperability between them and the existing module format, CommonJS. The following example of an ES module imports the function from addTwo.mjs: // app.mjs The following example of an ES module exports a function: // addTwo.mjs Modules are defined using a variety of import and export statements.
#Moduleloader plus code
ECMAScript modules are the official standard format to package JavaScript code for reuse.
