Stability: 1 - Experimental

    • Type: {string}

    The "type" field defines the module format that Node.js uses for all .js files that have that package.json file as their nearest parent.

    Files ending with .js are loaded as ES modules when the nearest parent package.json file contains a top-level field "type" with a value of "module".

    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 a node_modules folder or the volume root is reached.

    1. // package.json
    2. {
    3. "type": "module"
    4. }
    1. # In same folder as preceding package.json
    2. node my-app.js # Runs as ES module

    If the nearest parent package.json lacks a "type" field, or contains "type": "commonjs", .js files are treated as [CommonJS][]. If the volume root is reached and no package.json is found, .js files are treated as [CommonJS][].

    import statements of .js files are treated as ES modules if the nearest parent package.json contains "type": "module".

    1. // my-app.js, part of the same example as above
    2. import './startup.js'; // Loaded as ES module because of package.json

    Regardless of the value of the "type" field, .mjs files are always treated as ES modules and .cjs files are always treated as CommonJS.