Skip to content

Commit

Permalink
fix: print nice npm error message for npm: specifier as well (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Dec 20, 2022
1 parent a5dca3f commit 70071de
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
26 changes: 25 additions & 1 deletion node/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,31 @@ test('Prints a nice error message when user tries importing NPM module', async (
} catch (error) {
expect(error).toBeInstanceOf(BundleError)
expect((error as BundleError).message).toEqual(
`It seems like you're trying to import an npm module. This is only supported in Deno via CDNs like esm.sh. Have you tried 'import mod from "https://esm.sh/p-retry"'?`,
`It seems like you're trying to import an npm module. This is only supported via CDNs like esm.sh. Have you tried 'import mod from "https://esm.sh/p-retry"'?`,
)
} finally {
await cleanup()
}
})

test('Prints a nice error message when user tries importing NPM module with npm: scheme', async () => {
expect.assertions(2)

const { basePath, cleanup, distPath } = await useFixture('imports_npm_module_scheme')
const sourceDirectory = join(basePath, 'functions')
const declarations = [
{
function: 'func1',
path: '/func1',
},
]

try {
await bundle([sourceDirectory], distPath, declarations, { basePath })
} catch (error) {
expect(error).toBeInstanceOf(BundleError)
expect((error as BundleError).message).toEqual(
`It seems like you're trying to import an npm module. This is only supported via CDNs like esm.sh. Have you tried 'import mod from "https://esm.sh/p-retry"'?`,
)
} finally {
await cleanup()
Expand Down
8 changes: 7 additions & 1 deletion node/npm_import_error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class NPMImportError extends Error {
constructor(originalError: Error, moduleName: string) {
super(
`It seems like you're trying to import an npm module. This is only supported in Deno via CDNs like esm.sh. Have you tried 'import mod from "https://esm.sh/${moduleName}"'?`,
`It seems like you're trying to import an npm module. This is only supported via CDNs like esm.sh. Have you tried 'import mod from "https://esm.sh/${moduleName}"'?`,
)

this.name = 'NPMImportError'
Expand All @@ -19,6 +19,12 @@ const wrapNpmImportError = (input: unknown) => {
const [, moduleName] = match
return new NPMImportError(input, moduleName)
}

const schemeMatch = input.message.match(/Error: Module not found "npm:(.*)"/)
if (schemeMatch !== null) {
const [, moduleName] = schemeMatch
return new NPMImportError(input, moduleName)
}
}

return input
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/imports_npm_module_scheme/functions/func1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import pRetry from "npm:p-retry"

0 comments on commit 70071de

Please sign in to comment.