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

Error resolving path import specifiers #16

Open
aomarks opened this issue Jun 8, 2019 · 7 comments
Open

Error resolving path import specifiers #16

aomarks opened this issue Jun 8, 2019 · 7 comments
Assignees
Labels
Type: Bug Something isn't working

Comments

@aomarks
Copy link
Member

aomarks commented Jun 8, 2019

The middleware appears to be trying to resolve import specifiers that are already paths, and erroring that it cannot resolve them. It should be ignoring them altogether:

Unable to resolve Node module specifier "./index-1.js" due to { Error: Cannot find module './index-1.js' from '/Users/aomarks/git/benchmarks/polymer/'
    at Function.module.exports [as sync] (/Users/aomarks/git/benchmarks/node_modules/resolve/lib/sync.js:71:15)
    at Object.exports.resolveNodeSpecifier (/Users/aomarks/git/benchmarks/node_modules/koa-node-resolve/src/support/resolve-node-specifier.ts:26:44)
    at koa_module_specifier_transform_1.moduleSpecifierTransform (/Users/aomarks/git/benchmarks/node_modules/koa-node-resolve/src/koa-node-resolve.ts:41:51)
    at countedSpecifierTransform (/Users/aomarks/git/benchmarks/node_modules/koa-node-resolve/src/koa-module-specifier-transform.ts:133:26)
    at enter (/Users/aomarks/git/benchmarks/node_modules/koa-node-resolve/src/transform-js-module.ts:27:42)
    at NodePath._call (/Users/aomarks/git/benchmarks/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/Users/aomarks/git/benchmarks/node_modules/@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit (/Users/aomarks/git/benchmarks/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/Users/aomarks/git/benchmarks/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitMultiple (/Users/aomarks/git/benchmarks/node_modules/@babel/traverse/lib/context.js:85:17) code: 'MODULE_NOT_FOUND' 
@usergenic
Copy link
Collaborator

I went and added some tests to look at resolution of relative paths, including the specific case of resolving a relative path to a file that doesn't exist https://github.com/Polymer/koa-node-resolve/blob/resolve-module-specifier-for-file-without-imports-and-exports/src/test/resolve-node-specifier.test.ts#L84 which is the only case I could concoct where that error would come up.

Is there any chance that in your case the ./index-1.js file doesn't exist at the time of the resolution?

The node resolution algorithm actually can't ignore relative paths because of the way node module resolution algorithm still has to transform relative path specifiers by adding missing file extensions, for example. ./index-1 should convert to ./index-1.js by node module resolution rules, when the file is present, for example.

@usergenic
Copy link
Collaborator

I also added tests to verify this wasn't some issue with missing import/export specifiers in index-1.js which actually makes sense because node module resolution algorithm doesn't care about file contents except package.json AFAICT.

@kevinpschaaf
Copy link
Member

kevinpschaaf commented Jun 26, 2020

It looks like specifically when resolving either packages or relative paths in HTML from a URL with no subpath (e.g. http://localhost:8080 as opposed to http://localhost:8080/home or http://localhost:8080/index.html), then paths being resolved by transformHTML are resolved against the parent of the HTTP root instead of the HTTP root.

e.g.

server.ts:

...
app.use(nodeResolve({
  root: '/Users/me/server_root'
}));

index.html (being served from http://localhost:8080 root of server):

...
<script src="./node_modules/package/module.js"></script>

I believe this is due to a combination of these two lines:

Afaict this is not a problem when the baseURL is a file, as in URLs in HTML with file parts, or imports from JS where there's always a file present.

@usergenic
Copy link
Collaborator

usergenic commented Jul 6, 2020

@kevinpschaaf I am having trouble reproducing this issue. Can you provide a repro? Is it worth mentioning that in your example, the script tag doesn't have type=module?

@usergenic
Copy link
Collaborator

I just added unit to koa-node-resolve.test.ts that specifically handles routing an html file through a path of '/' and this is showing failures to resolve; so this may be a sufficient repro case. WIP

@usergenic
Copy link
Collaborator

Thanks, @kevinpschaaf I have a PR to fix this now in review #30

@usergenic usergenic self-assigned this Jul 6, 2020
@usergenic usergenic added the Type: Bug Something isn't working label Jul 6, 2020
@usergenic
Copy link
Collaborator

usergenic commented Jul 7, 2020

@aomarks Does the published 1.0.0-pre.8 version with the #30 fix solve the problem you encountered when you posted this...a year ago 😲?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants