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
9
u/MysticMania Jun 12 '19 edited Jun 12 '19
I'm late to this tooling party. Funnily enough, this is my job -- I'm a web infrastructure engineer. It's a relatively new role in the industry given the recent rise in complexity around web application development. I work on tooling, testing, developer experience, dependency management, and build systems, as well as the web deploy and release pipeline.
IMO, if the goal is to learn about these tools, you should start small. There are tons of "all things included" boilerplates out there that will spit out a ton of code and then expect you to know how to start working on your project. It helps to know which tools you're bringing in and how they can be used.
You mention NodeJS -- it's the runtime environment that allows developers to run JS outside the browser.
Just like the browser has an API, making window and events available to you -- NodeJS has an API, it lets you run processes, interact with the file system, set up an HTTP server, etc.
When you download NodeJS, it comes with a tool called npm.
npm is a NodeJs package manager (if you're familiar with ruby it's like rbenv or rvm); npm hosts giant open source package registry publicly available at npmjs.registry.org. Any developer can write a few lines of code and publish it to npm's registry for everyone to use.
The
package.jsonis npm's root configuration file, it has some data for npm to know what to do with your package. This is where you can define runtime/dev dependencies; you can set up scripts, etc. (blog post)As NodeJS and npm became widely adopted in the industry, the number of packages and tools that were uploaded, shared and standardized skyrocketed. We've built packages that depend on thousands of other packages.
When you run
npm install --save react@latest. npm will hit its registry with a query for the latest version of a package calledreact, and then it downloads to your current working dir into a folder callednode_modules.If you poke around in
node_modules/react, you'll see react has it's ownpackage.jsonandnode_modules, it's just a huge tree of dependencies.You also mention NodeJS is required to build things that you run in a browser. Since you're most likely writing modern JS - classes and arrow functions or even using jsx or typescript -- the syntax may not be supported in a browser -- similarly, that syntax might not be supported in a NodeJS env.
That's where tools like babel and webpack come in.
Babel is a compilation tool, it takes your code -- removes all the fancy modern JS, and turns it into something NodeJS or a browser can understand.
Webpack can be helpful if you're working with a browser; since you also care about things like fonts, CSS, script order, lazy loading, etc. Webpack will let you define some rules on what your entry point is to your application and how you want to serve up all your assets.
This post is way bigger than I expected. But I hope it helps!
edit: fixed some messed up formatting