Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

"Warning: require() of ES modules is not supported" when package.json has "type":"module" #855

@yoursunny

Description

@yoursunny

Environment: Node 12.13.1, both Windows 10 and Ubuntu 16

Snippet to reproduce:
package.json

{
  "dependencies": {
    "esm": "*"
  },
  "private": true,
  "type": "module"
}

module.js

export const X = 1;

main.cjs

require = require("esm")(module);
const m = require("./module.js");
console.log(m.X);

Steps to reproduce:

  1. Save the above three files in the same directory.
  2. npm install
  3. node main.cjs

Expected: console prints 1 and nothing else.

Actual:

$ node main.cjs
1
(node:8464) Warning: require() of ES modules is not supported.
require() of C:\Users\sunny\Documents\code\esmbug\module.js from C:\Users\sunny\Documents\code\esmbug\main.cjs is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package 
scope as ES modules.
Instead rename module.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:\Users\sunny\Documents\code\esmbug\package.json.

Observations:

  • The program is running fine except for the warning message.
  • The warning only appears when the program is running for the first time. It does not reappear, unless node_modules/.cache directory is deleted.
  • Node 12.13.1 is the first time this warning appears; it did not appear in Node 12.13.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions