List modules bundled by Browserify - browserify

I am using Browserify and Grunt to bundle our RendrJS webapp. I would like to analyze the bundle to see which modules it contains to see if any code is superfluous. This can happen since in Rendr some js code might only run on the server.
How do I get a list of the contents of the bundle. I have tried:
browserifyOptions: { 'list': true }
but it doesn't seem to have any effect. We are using Browserify 2.

Saw your question, dug up a tiny old script and just now pushed browserify-inspect-bundle to npm, grab it here:
https://www.npmjs.com/package/browserify-inspect-bundle
Usage is:
npm install -g browserify-inspect-bundle
browserify-inspect-bundle /path/to/your/bundle.js
This should get you something useful.

Related

Blazor Javascript isolation with NPM dependencies

I'm trying to use the new Blazor Javascript isolation feature. I'm importing my own JS file as per the example ExampleJsInterop.cs. It works until I try to import an NPM module from within my script. In my package.json I have set up a dependency on interactjs, and in my script I have added import interact from 'interactjs'; at the top.
I'm getting a Failed to resolve module specifier "interactjs" error. I'm not sure how to get past that.
Previously I was using Webpack to bundle my script and dependencies together into a single file that is added into my index.html as a tag. This was working fine, but I'm not sure how to continue using NPM packages with JS isolation.
Thanks!
A bit late, but I've just finished solving a similar issue.
The npm files are installed to the hidden node_modules folder. This isn't available to your script when you are running your app, unless you do something to make it available. however, even if you copied the interactjs file into your scripts folder it would still not work if it was an npm file. Those are meant to run in nodejs not a browser. So you would still need to use your bundler. I tried webpack, but had some issues with certain files so ended up with snowpack instead. I just finished a bunch of articles on javascript interop - part 4 deals with npm
I forgot that I left this question open for almost a year!
I ended up solving it using Snowpack to bundle the NPM package into the Blazor wwwroot folder. Credit goes to this article for pointing me in the right direction: https://nbarraud.github.io/js-in-blazor.html

How to change src code of Vue in node_modules for testing

I am using Vue 2 (doesn't really matter which version exactly).
I want to test some things that happen behind the hood in Vue. So I decided to add console.log('test123') in Vue's files in node_modules. It turns out that the console log never fires. I put that in all files of Vue in node_modules, even in all files of dist's folder of Vue.
How can I achieve this ? If I fork the repo, then I'd have to upload new versions each time on my repo and then run npm install. I know that will work but wanted to achieve this without forking.
Any ideas what I am missing ?
there are many ways .. but i feel more comfortable using this method :
you can download any npm package in a seperated folder next to your project...
open the folder of the package then run this in the terminal:
npm link
then open the project folder and run
npm link ../package-path # link the dir of your dependency
References
npm-link
How to test an npm package locally

How to use Font Awesome after it being installed with Yarn

I am using VS 2019 for Core 3.1 development and I installed Font Awesome whith Yarn:
yarn add #fortawesome/fontawesome-free
However, whenI try to reference it in my HEAD section like this:
<script defer src="~/lib/#fortawesome/fontawesome-free/js/all.js"></script>
I get the following error:
the 'fortawesome' doesnt exist in actual context
Package managers like yarn, npm, etc. just add the packages to your project. They generally aren't ready to deploy directly at that point, but rather require a build pipeline to create the actual JS/CSS resources. The #fortawesome/fontawesome repo is an exception in that 1) it's not actually a package and 2) the files are already "built". Even then, though, they still won't be in the right location.
I'm not overly familiar with yarn, but npm, for example, puts everything in a node_modules directory. That directory is not served by default, and should not be served, because it contains "raw" stuff. You'd need a separate build pipeline (using npm scripts, webpack, gulp, etc.) to build/copy assets into a directory that is served (i.e. wwwroot). That is likely the piece you are missing.
For this, since there's no build actually required, you just need to have the assets copied to something like wwwroot/lib and then you'll be able to reference it via the script tag on the page. That can be done with any tool like npm, webpack, gulp, grunt, etc. so you'll just have to research and pick the one that fits your needs best.

Why I have too many packages inside my node_modules?

I am new, and when i first created my app based on the documentation using npm create-react-app i found it there were a lot of package included inside folder node_module when i code and i only use react and react DOM and etc from the basic.
node_modules
acorn
timer
ansi
and many more
I wonder if anyone can help my how to understand each use inside the node_module or where can i find the documentation for each use?
or how can i just reduce to what i want to use only to decrease the app size?
The answers are 2:
because you're using an automated scaffolding tool, which essentially does everything for you, and, you have just to code, it is supposed to locally deploy all the packages it needs to work (for example webpack is needed to bundle your code, babel to transpile it, ...
under node_modules you will find all the packages of the whole app. That's means you will find both your dependencies and the dependencies of your dependencies (this rule has some exceptions and you can find them in the npm documentation.
example:
// your code depends on A
var dependency = require('A');
// but then, inside your A dependency you can also find something similar to:
var b = require('B');
how can i just reduce to what i want to use only to decrease the app size?
You basically can't do it. They are all needed.
Most of the libraries that we pull from npm have dependencies. You may use only react and react-dom but there are react-scripts that require lots of stuff. I don't think that you have to worry about the size of the node_modules. That's not what you are suppose to ship in production.
If you want to see what are these all modules about you may open their folder and fine README.md file.

How do I view the size of npm packages?

When I search for packages on NPM, I would like to see package sizes (in KB or MB, etc). NPM doesn’t seem to show this information.
How can I determine how much bloat an NPM package will add to my project?
What you probably want to measure is the impact a package has if you were to add it to your app bundle. Most of the other answers will estimate the size of the source files only, which maybe inaccurate due to inline comments, long var names etc.
There is a small utility I made that'll tell you the min + gzipped size of the package after it gets into you bundle -
https://bundlephobia.com
Take a look at this cost-of-modules project. It's an npm package that will list the size of a package and number of children.
Installation:
npm install -g cost-of-modules
Usage:
Run cost-of-modules in the directory you are working in.
I created Package Phobia early this year with the hope to get the package size information into npmjs.com and also track package bloat over time.
https://packagephobia.com
This is designed to measure disk space after you run npm install for server-side dependencies like express or dev dependencies like jest.
You can read more about this tool and other similar tools in the readme here: https://github.com/styfle/packagephobia
Update 2020
The "Unpacked Size" (basically Publish Size) is available on the npmjs.com website along with "Total Files". However, this is not recursive meaning that npm install will likely be much bigger because a single package likely depends on many packages (thus Package Phobia is still relevant).
There is also a pending RFC for a feature which prints this information from the CLI.
In case you are using webpack as your module bundler have a look at:
webpack-bundle-analyzer
webpack-bundle-size-analyzer
I definitely recommend the first option. It shows size in interactive treemap. This helps you to find the size of package in your bundled file.
The other answers in this post show you size of the project, but you might not be using all parts of the project, for example with tree shaking. Other approaches then might not show you accurate size.
I've created a tool, npm download size, which inspects tarball size for a given npm package, including all tarballs in the dependency tree. This gives you an idea of the cost (install time, disk space, runtime resources, security audit, ...) of adding the dependency up front.
In image above, Tarball size is tar.gz of package, and Total size is size of all tarballs. The tool is pretty basic, but it does what it says.
A cli tool is also available. You can install it like this:
npm i -g download-size
And use it like this:
$ download-size request
request#2.83.0: 1.08 MiB
The source code is available on Github: api, cli tool and web client.
Try to use package-size.
npx package-size vue,vue-router,vuex react,react-dom,react-router,redux
https://github.com/egoist/package-size
howfat is one more tool which can show total package size:
npx howfat jasmine
If you use Visual Studio Code, you could use an extension called Import Cost.
This extension will display inline in the editor the size of the imported package. The extension utilizes webpack with babili-webpack-plugin in order to detect the imported size.
You could check out npm-module-stats. It is an npm module that gets the size of an npm module and its dependencies without installing or downloading the module.
Usage:
var stats = require("npm-module-stats");
stats.getStats("glob").then((stack) => {
let dependencies = Object.keys(stack);
let totalSize = dependencies.reduce((result, key, index) => {
return result + stack[key].size;
}, 0);
console.log('Total Size in Bytes ', totalSize);
console.log('Total Dependencies ', dependencies.length-1);
}).catch((err) => {
console.error(err);
});
It might seem a little verbose but it solves the problem you described appropriately.
Before publishing the npm package, You can check the size of the package using the following command.
npm publish --dry-run
I have attached the result of my npm package.
A "quick & dirty" way is to use curl and wzrd.in to quickly download the minified package and then grep the file size:
curl -i https://wzrd.in/standalone/axios#latest | grep Content-Length
The download is minified but not gzipped, but you get a good idea of the relative size of packages when you compare two or more of them.
I prefer https://github.com/aholachek/bundle-wizard all the way since it was released.
It works on deployed sites: npx bundle-wizard reddit.com
It works on your local project:
For multi-page apps/sites adjust the last line with the path you want to check.
npm run build
npx serve -s build
npx bundle-wizard localhost:5000/
The interactive view is really helpful in discovering what's where.
In order to check the impact of different packages on your bundle. You can check out source-map-explorer.
Install:
npm install -g source-map-explorer
Usage:
source-map-explorer bundle.min.js
source-map-explorer bundle.min.js bundle.min.js.map
source-map-explorer bundle.min.js*
source-map-explorer *.js
This will open up a visualization of how space is used in your minified bundle.
I created bundlejs for this exact reason. Bundlejs was designed to be a fast and always accessible online bundler that can treeshake, analyze the bundle, minify, gzip & brotli the bundle, give the compressed size, and also return the fully bundled code, you can check it out at https://bundlejs.com.
The source code is available on GitHub at https://github.com/okikio/bundlejs
However, if you're goal is to just check what the npm install size for packages, I highly recommend https://packagephobia.com. I didn't create packagephobia but it's a high quality tool that covers the other niche when it comes to checking js package sizes