Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe copy new ERR_REQUIRE_ESM error messaging from node #1407

Open
Tracked by #1504
cspotcode opened this issue Jul 21, 2021 · 1 comment
Open
Tracked by #1504

Maybe copy new ERR_REQUIRE_ESM error messaging from node #1407

cspotcode opened this issue Jul 21, 2021 · 1 comment

Comments

@cspotcode
Copy link
Collaborator

nodejs/node@e2a6399#diff-670bf55805b781d9a3579f8bca9104c04d94af87cc33220149fd7d37b095ca1cR1440-R1466

@cspotcode
Copy link
Collaborator Author

cspotcode commented Jul 22, 2021

filename, hasEsmSyntax, parentPath = null, packageJsonPath = null

require() of ES Module ${filename}${parentPath ? ` from ${
      parentPath}` : ''} not supported.
    if (!packageJsonPath) {
      if (StringPrototypeEndsWith(filename, '.mjs'))
        msg += `\nInstead change the require of ${filename} to a dynamic ` +
            'import() which is available in all CommonJS modules.';
      return msg;
    }
    const path = require('path');
    const basename = path.basename(filename) === path.basename(parentPath) ?
      filename : path.basename(filename);
    if (hasEsmSyntax) {
      msg += `\nInstead change the require of ${basename} in ${parentPath} to` +
        ' a dynamic import() which is available in all CommonJS modules.';
      return msg;
    }
${basename} is treated as an ES module file as it is a .js 
file whose nearest parent package.json contains "type": "module" 
which declares all .js files in that package scope as ES modules.

Instead rename ${basename} to end in .cjs, change the requiring 
code to use dynamic import() which is available in all CommonJS 
modules, or change "type": "module" to "type": "commonjs" in 
${packageJsonPath} to treat all .js files as CommonJS (using .mjs for 
all ES modules instead).

We can detect when it's .ts or .tsx and adapt the message to explain how our rules work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant