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

Does not resolve to files in node_modules lower than root #2

Open
remko opened this issue Oct 29, 2021 · 1 comment
Open

Does not resolve to files in node_modules lower than root #2

remko opened this issue Oct 29, 2021 · 1 comment

Comments

@remko
Copy link

remko commented Oct 29, 2021

Steps to reproduce:

# npm install bootstrap

Root file works fine:

# ls node_modules/bootstrap/LICENSE
node_modules/bootstrap/LICENSE
# node
> require('resolve-file')('bootstrap/LICENSE')
'/path/to/node_modules/bootstrap/LICENSE'

Lower file does not:

# ls node_modules/bootstrap/dist/js/bootstrap.js
node_modules/bootstrap/dist/js/bootstrap.js

# node
> require('resolve-file')('bootstrap/dist/js/bootstrap.js')
null
@baurine
Copy link

baurine commented Dec 3, 2021

The similar issue, it is not that it can't resolve files lower than the root folder, but it can't resolve the files that are not the entry files and are not under the root folder.

But it works if we call the require.resolve() directly.

Test code:

const resolveFile = require('resolve-file')

console.log(resolveFile('esbuild-plugin-postcss2/LICENSE'))
// output: /repo_path/node_modules/esbuild-plugin-postcss2/LICENSE

console.log(resolveFile('esbuild-plugin-postcss2/dist/index.js'))
// output: /repo_path/node_modules/esbuild-plugin-postcss2/dist/index.js

console.log(resolveFile('esbuild-plugin-postcss2/dist/index.esm.js'))
// output: /repo_path/node_modules/esbuild-plugin-postcss2/dist/index.esm.js

console.log(resolveFile('esbuild-plugin-postcss2/src/modules.d.ts'))
// output: null

// directly use require.resolve
console.log(require.resolve('esbuild-plugin-postcss2/src/modules.d.ts'))
// output: /repo_path/node_modules/esbuild-plugin-postcss2/src/modules.d.ts

I don't understand the below code logic very much:

resolve-file/index.js

Lines 98 to 106 in f598211

if (!utils.exists(file.path)) {
try {
if (/[\\\/]/.test(name)) {
file.basename = path.basename(name);
var dirname = path.dirname(name);
file.name = path.basename(dirname);
file.main = require.resolve(dirname);
file.path = path.resolve(path.dirname(file.main), file.basename);
}

  if (!utils.exists(file.path)) {
    try {
      if (/[\\\/]/.test(name)) {
        file.basename = path.basename(name);
        var dirname = path.dirname(name);
        file.name = path.basename(dirname);
        file.main = require.resolve(dirname);
        file.path = path.resolve(path.dirname(file.main), file.basename);
      }

Why do we call require.resolve(dirname), not require.resolve(name)? If we call require.resolve(name) then we can get the expected result.

@doowb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants