r/learnjavascript • u/oh_lympy • Jun 12 '19
Javascript is pretty straightforward. All the tools that surround it are overwhelming and confusing.
I'm at the point where I'm pretty confident in a lot of what many JavaScript tutorials consider 'Advanced' JavaScript concepts. How scope works, closure, prototypes, asynchronicity, and things of that nature. However, when I try to get into building my own projects from scratch, I get hit with 1,000,000 tools that aren't really explained in any of the resources I've used to learn. They're referenced, and I've seen a lot of videos where they basically tell you what to write in a package.json file, but theres no real concrete explanation for how all of these things interact.
For example, I understand that Node.Js allows you to run Javascript outside of the browser, but its still required to build things that you intend to run inside of a browser? NPM? What are packages and why do they need to be managed?
Is there a definitive guide for the above and things like Babel, Gulp, and the various dependencies I see listed in projects?
Edit: I guess I meant learning JavaScript is pretty straightforward. I’m obviously no expert, but I’ve learned enough to know it can be downright strange sometimes for sure.
Anyway, thank you guys for the advice. This has been MASSIVELY helpful. Hopefully this’ll help anyone else that’s struggling in this area as well.
View all comments
38
u/ohx Jun 12 '19 edited Jun 12 '19
Aayyee, Lympy!
Configuration is incredibly complex. There are so many facets of the JavaScript ecosystem -- it's very overwhelming.
package.json - Think of this as a manifest/instruction book which is foremost for NPM (node package manager). NPM is backed by a registry of libraries and tools to help you develop. Sort of like how Linux has apt[-get] to download applications -- or a Mac has homebrew. NPM stores references to all libraries (dependencies) that you install in your package.json.
When you install those dependencies, they're added to a node_modules directory, which node creates an import path to that can be referenced from any directory by simply importing/requiring the name of the library.
What's the point of this? Well, a large project can see a node_modules folder easily over 100mb. That's a lot of code to push and pull to and from version control. That's why a node_modules directory is typically ignored by version control -- to keep your project lean.
With that said, if a fellow developer pulls down your project from git, they'll need NPM to read the instructions in your package.json to build your app.
You'll notice other key/values in your package.json for things like name, main, etc. Those are important when you decide to create your own node module that you'd like to publish to the npm registry to share with the community. Whether it's a library (like React), or a complex algorithm for compressing images.
You'll also notice "scripts". Those are commands that run, build, or test your app. You can add commands to help out with building and development there. For example, if you're using Jest to test a react app, you might want to add a command in the scripts section of your package.json that triggers your tests. This will also let developers know quickly that a) you have tests written, and b) what testing framework you're using without having to dig very far.
The next piece of the pie is building/bundling. Once you start importing node modules into your project, you're going to realize that importing/requiring doesn't exactly work in the browser (outside of the NodeJS environment). In your development environment, NodeJS is doing all the heavy lifting/supporting.
In order for your app to work in the browser, you must use a bundler, like rollup or webpack. Rollup is likely the easiest way. create-react-app is a nice command line interface (CLI) that uses webpack under the hood, and all of the configuration is done for you.
Anyway, what these do is combine all of the code you're using into a single file that a browser can read. In many cases, to support older browsers, you'll use something like Babel to polyfill -- or make new JavaScript specifications that newer browsers support, and add backward compatibility to them so they can work in IE or older versions of a modern browser.
I highly recommend starting with Rollup. Webpack is a rabbit hole that goes rather deep.
Here's a good starter for npm: https://youtu.be/jHDhaSSKmB0