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.

Mark all node_modules as external

See original GitHub issue

tsup right now has an issue with node modules with files inside the module that are not the main file (for example, import { foo } from "bar/baz"), and it always tries to bundle them, which pretty much always is a non-desired behavior. And even breaks when outputting to esm, since it tries to bundle commonjs inside the esm modules

evanw gave a solution as a plugin mentioned here: https://github.com/evanw/esbuild/issues/619#issuecomment-751995294

I tested it locally and it works fine, but I would need some feedback in how to enable this behaviour, the cleanest one I could thought off is reading the moduleResolution: "node" in the tsconfig, since having that it should assume you are dealing with outputting for Node.js, but maybe a very small flag in the CLI would be a good solution too, like “tsup src/index.ts --node”

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
mindplay-dkcommented, Mar 22, 2022

tsup right now has an issue with node modules with files inside the module that are not the main file (for example, import { foo } from "bar/baz"), and it always tries to bundle them, which pretty much always is a non-desired behavior.

Why? I thought bundling dependencies was the point of using a bundler?

I found this in the README as well:

By default tsup bundles all import-ed modules but dependencies in your packages.json are always excluded

This also doesn’t explain why? (It probably should?)

My simple use-case is a library that internally uses fast-deep-equal - I consider that an implementation detail, not something someone should need to install; I want to ship a single .js file with no dependencies, with the expected version of this library built-in, and so on.

Is this unusual? It seems to be default behavior of other bundlers, e.g. webpack, rollup - and for that matter esbuild, which it sounds like you needed a plugin or workaround to suppress that?

In my experience, dependencies are usually bundled by default, unless listed as externals somehow - whereas peerDependencies are not?

I found this very confusing - and the manual doesn’t seem to mention any opt-out?

If you’re not going to follow the typical pattern of bundlers bundling by default, with externals as an opt-out, perhaps you might consider an internals switch to reverse this behavior for libraries where you do want to bundle something?

2reactions
mindplay-dkcommented, Mar 22, 2022

For the record, moving this dependency to devDependencies fixed it - and it’s probably the right solution, since this is not a dependency in the npm sense, but only a build-time dependency.

Putting it there feels a bit odd, as it’s not the advice you’re normally given - but it also kind of makes sense, given that it won’t be an install-time dependency.

If that’s the correct approach, this should probably be mentioned in documentation? It probably won’t be obvious to most users, given that this is not how most bundlers behave by default?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Option to treat all node modules as external #619 - GitHub
I think what is desirable for this proposed option would be if ESBuild treated every package in node_modules/ as an external (or perhaps ......
Read more >
How to set as external all node modules in rollup?
How to set as external all node modules in rollup? ; from "path" · import glob ; from "glob" · import multiEntry ;...
Read more >
How to use External Modules and NPM in a project
Local/File-based modules: It define the Node modules within a file in our application and is used within our application. · Core modules: The ......
Read more >
webpack-node-externals - npm
This library scans the node_modules folder for all node_modules names, and builds an externals function that tells Webpack not to bundle those ...
Read more >
Using external node dependencies - Phantombuster
A bundler is a tool which takes all your code, dependencies and assets ... colors npm package - a very popular module to...
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