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

module: better error for named exports from cjs #33256

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/internal/modules/esm/module_job.js
@@ -1,10 +1,14 @@
'use strict';

const {
ArrayPrototypeJoin,
ObjectSetPrototypeOf,
PromiseAll,
SafeSet,
SafePromise,
StringPrototypeIncludes,
StringPrototypeMatch,
StringPrototypeSplit,
} = primordials;

const { ModuleWrap } = internalBinding('module_wrap');
Expand Down Expand Up @@ -93,6 +97,29 @@ class ModuleJob {
}
} catch (e) {
decorateErrorStack(e);
if (StringPrototypeIncludes(e.message,
' does not provide an export named')) {
const splitStack = StringPrototypeSplit(e.stack, '\n');
const parentFileUrl = splitStack[0];
const childSpecifier = StringPrototypeMatch(e.message, /module '(.*)' does/)[1];
const childFileURL =
await this.loader.resolve(childSpecifier, parentFileUrl);
const format = await this.loader.getFormat(childFileURL);
if (format === 'commonjs') {
const importStatement = splitStack[1];
const namedImports = StringPrototypeMatch(importStatement, /{.*}/)[0];
e.message = `The requested module '${childSpecifier}' is expected ` +
'to be of type CommonJS, which does not support named exports. ' +
'CommonJS modules can be imported by importing the default ' +
'export.\n' +
'For example:\n' +
`import pkg from '${childSpecifier}';\n` +
`const ${namedImports} = pkg;`;
const newStack = StringPrototypeSplit(e.stack, '\n');
newStack[3] = `SyntaxError: ${e.message}`;
e.stack = ArrayPrototypeJoin(newStack, '\n');
}
}
throw e;
}
for (const dependencyJob of jobsInGraph) {
Expand Down
64 changes: 64 additions & 0 deletions test/es-module/test-esm-cjs-named-error.mjs
@@ -0,0 +1,64 @@
import '../common/index.mjs';
import { rejects } from 'assert';

const fixtureBase = '../fixtures/es-modules/package-cjs-named-error';

const expectedRelative = 'The requested module \'./fail.cjs\' is expected to ' +
'be of type CommonJS, which does not support named exports. CommonJS ' +
'modules can be imported by importing the default export.\n' +
'For example:\n' +
'import pkg from \'./fail.cjs\';\n' +
'const { comeOn } = pkg;';

const expectedPackageHack = 'The requested module \'./json-hack/fail.js\' is ' +
'expected to be of type CommonJS, which does not support named exports. ' +
'CommonJS modules can be imported by importing the default export.\n' +
'For example:\n' +
'import pkg from \'./json-hack/fail.js\';\n' +
'const { comeOn } = pkg;';

const expectedBare = 'The requested module \'deep-fail\' is expected to ' +
'be of type CommonJS, which does not support named exports. CommonJS ' +
'modules can be imported by importing the default export.\n' +
'For example:\n' +
'import pkg from \'deep-fail\';\n' +
'const { comeOn } = pkg;';

rejects(async () => {
await import(`${fixtureBase}/single-quote.mjs`);
}, {
name: 'SyntaxError',
message: expectedRelative
}, 'should support relative specifiers with single quotes');

rejects(async () => {
await import(`${fixtureBase}/double-quote.mjs`);
}, {
name: 'SyntaxError',
message: expectedRelative
}, 'should support relative specifiers with double quotes');

rejects(async () => {
await import(`${fixtureBase}/json-hack.mjs`);
}, {
name: 'SyntaxError',
message: expectedPackageHack
}, 'should respect recursive package.json for module type');

rejects(async () => {
await import(`${fixtureBase}/bare-import-single.mjs`);
}, {
name: 'SyntaxError',
message: expectedBare
}, 'should support bare specifiers with single quotes');

rejects(async () => {
await import(`${fixtureBase}/bare-import-double.mjs`);
}, {
name: 'SyntaxError',
message: expectedBare
}, 'should support bare specifiers with double quotes');

rejects(async () => {
await import(`${fixtureBase}/escaped-single-quote.mjs`);
}, /import pkg from '\.\/oh'no\.cjs'/, 'should support relative specifiers with escaped single quote');
@@ -0,0 +1 @@
import { comeOn } from "deep-fail";
@@ -0,0 +1 @@
import { comeOn } from 'deep-fail';
@@ -0,0 +1 @@
import { comeOn } from "./fail.cjs";
@@ -0,0 +1 @@
import { value } from "./oh'no.cjs";
3 changes: 3 additions & 0 deletions test/fixtures/es-modules/package-cjs-named-error/fail.cjs
@@ -0,0 +1,3 @@
module.exports = {
comeOn: 'fhqwhgads'
};
@@ -0,0 +1 @@
import { comeOn } from './json-hack/fail.js';
@@ -0,0 +1,3 @@
module.exports = {
comeOn: 'fhqwhgads'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

everybody to the limit

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe foo would be more appropriate here?

Copy link
Member

@ljharb ljharb May 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't see how anything could possibly be more appropriate than this particular reference

};
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/es-modules/package-cjs-named-error/oh'no.cjs
@@ -0,0 +1,3 @@
module.exports = {
value: 123
};
5 changes: 5 additions & 0 deletions test/fixtures/es-modules/package-cjs-named-error/package.json
@@ -0,0 +1,5 @@
{
"name": "package-cjs-named-error",
"main": "index.mjs",
"type": "module"
}
@@ -0,0 +1 @@
import { comeOn } from './fail.cjs';