question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible bug: relative paths in the "browser" field are interpreted as package paths

See original GitHub issue

Context: I’m the author of esbuild, a JavaScript bundler, and I’m currently working on a bug in esbuild related to the browser field: https://github.com/evanw/esbuild/issues/740. The discussion referenced behavior differences between Browserify and Webpack. I believe the problem in this issue is a bug in Browserify, so I am re-reporting it here in case you weren’t already aware of it.

A demonstration of the bug is here: https://github.com/evanw/browserify-package.json-browser-bug. This contains a call to require('foo/bar') and a package.json file with "browser": { "./foo/bar": "./foo_bar.js" }. Webpack does not apply this remapping but Browserify does. This seems like a bug in Browserify to me since there is nothing at the path ./foo/bar. The path to the underlying file this case is either a relative path of ./node_modules/foo/bar or a package path of foo/bar but not a relative path of ./foo/bar.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, Feb 22, 2021

Nothing need be present at that location. In this example, require('package/foo/bar') would throw in node but load package/foo_bar.js in the browser.

0reactions
dsyercommented, Mar 15, 2022

I think I have encountered this bug as well. I see a lot of places in browserify where '/' is prepended to a package path. So if you do this

$ node_modules/browserify/bin/cmd.js -r ./hello.js > bundle.js

then bundle.js has this

require=(function()...)()({"/hello.js":[...]},{},[]);

I.e. the leading ‘./’ is stripped and replaced with ‘/’. That explains why a relative path and a package path are equivalent. It also breaks require('./hello.js') in code that imports bundle.js. I can see tests in browserify (like externalize) which appear to work but I can’t really understand how.

The example in the README (https://github.com/browserify/browserify#external-requires) would also break if you wanted to require('./my-file.js') instead of require('my-module').

Read more comments on GitHub >

github_iconTop Results From Across the Web

webpack: Module not found: Error: Can't resolve (with relative ...
Your file structure says that folder name is Container with a capital C. But you are trying to import it by container with...
Read more >
Relative and absolute paths, in the file system and on the web ...
For the browser, this path is perfectly absolute, starting from the root of the web-server. Whereas for the script it's only a part...
Read more >
Documentation - Module Resolution - TypeScript
Use non-relative paths when importing any of your external dependencies. ... There are two possible module resolution strategies: Node and Classic.
Read more >
File.webkitRelativePath - Web APIs | MDN
The File.webkitRelativePath is a read-only property that contains a string which specifies the file's path relative to the directory ...
Read more >
00141: Referencing data with relative paths is not supported ...
Error 00141: Referencing data with relative paths is not supported. ... two ways—one is to package the data with the map document, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found