ts-node icon indicating copy to clipboard operation
ts-node copied to clipboard

Maybe copy new ERR_REQUIRE_ESM error messaging from node

Open cspotcode opened this issue 4 years ago • 1 comments
trafficstars

https://github.com/nodejs/node/commit/e2a6399be742f53103474d9fc1e56fadf7f90ccc#diff-670bf55805b781d9a3579f8bca9104c04d94af87cc33220149fd7d37b095ca1cR1440-R1466

cspotcode avatar Jul 21 '21 05:07 cspotcode

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.

cspotcode avatar Jul 22 '21 02:07 cspotcode