Skip to content

Commit 62c14cb

Browse files
authoredMay 3, 2023
fix: don't print esm warning, if package name is not found (#3292)
1 parent 19fcd8d commit 62c14cb

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed
 

‎packages/vitest/src/node/error.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,14 @@ function handleImportOutsideModuleError(stack: string, ctx: Vitest) {
150150
else
151151
name = name.split('/')[0]
152152

153-
ctx.logger.error(c.yellow(
153+
if (name)
154+
printModuleWarningForPackage(ctx.logger, path, name)
155+
else
156+
printModuleWarningForSourceCode(ctx.logger, path)
157+
}
158+
159+
function printModuleWarningForPackage(logger: Logger, path: string, name: string) {
160+
logger.error(c.yellow(
154161
`Module ${path} seems to be an ES Module but shipped in a CommonJS package. `
155162
+ `You might want to create an issue to the package ${c.bold(`"${name}"`)} asking `
156163
+ 'them to ship the file in .mjs extension or add "type": "module" in their package.json.'
@@ -170,6 +177,13 @@ function handleImportOutsideModuleError(stack: string, ctx: Vitest) {
170177
}\n`)))
171178
}
172179

180+
function printModuleWarningForSourceCode(logger: Logger, path: string) {
181+
logger.error(c.yellow(
182+
`Module ${path} seems to be an ES Module but shipped in a CommonJS package. `
183+
+ 'To fix this issue, change the file extension to .mjs or add "type": "module" in your package.json.',
184+
))
185+
}
186+
173187
export function displayDiff(diff: string, console: Console) {
174188
console.error(diff)
175189
}

0 commit comments

Comments
 (0)
Please sign in to comment.