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.
24
u/True_Scorpio23 Jun 12 '19
It’s not just JavaScript tools. No matter what language or tech stack you choose you will have to configure your projects for production. That is where all the different tools come into play. You have more than one option. That is why a tool like create-React-app is great for beginners (like myself) because it does it all for you and you can get to coding and not worry about dependencies, modules, etc. I suggest looking at the create-React-app tool. See what comes with it, what each tool accomplished and then look at other tools that do the same thing to start to see what the ecosystems is really like.
3
Jun 12 '19
What's the difference between using create-react-app and the cdn link of react directly in your html files?
5
u/ParkerZA Jun 12 '19
CRA sets up Webpack, Babel and a host of other stuff for you. It's basically just a template to begin with so you don't have to do all that stuff yourself. You can always eject it if you wish to configure it to your liking though
5
u/ForScale Jun 12 '19 edited Jun 12 '19
A whole freakin lot. Create react app spins up a whole development environment for you, while linking to react source just makes react code available.
1
Jun 13 '19
Create react app spins up a whole development environment for you,
Okay.
while linking to react source just makes react code available.
But isn't that what we want? Just write React Code
1
u/ForScale Jun 13 '19
Depends on your goals. If you want to have a pleasant development experience using things like a local server and hot module reloading and typescript and Babel and linting and compilation for optimized production builds, then you're going to have to set all that up. And things like create react app will do all of that for you.
3
Jun 12 '19
Create react app is awesome. It creates a local server for you, and includes auto refresh. Its honestly pretty damned nice, I was surprised.
2
Jun 12 '19
Yeah but youll be better off learning this shit slowly and learning to build react apps from scratch, how to import export modules, how to define separation of concerns and organize your program into various files and folders etc. Going from nothing to create react app is a huge leap and unless you understand what its all doing and why, youre not saving yourself that much time.
-5
Jun 12 '19
It does everything for you. There is a problem there. If something breaks. You won't have any idea on where to look for. Create-react-app is bloated with things most beginners don't need.
11
u/heterosapian Jun 12 '19 edited Jun 12 '19
Like what specifically?
Major issues with create-react-app, like any popular boilerplate, are quickly patched. Community support and testing are massive advantages over completely reinventing the wheel for every single project.
Newcomers will not be able to write something better (since a lot of features are quality of life improvements which any newcomer wouldn’t even be aware of) and they will invariably struggle with just getting basic setup for webpack. Good to struggle through things eventually but, if you’re trying to learn react, it’s best not to attempt to learn all how a modern js app should be setup beforehand.
In the same way if you want to learn to make a game, you learn Unity or a framework with high level abstractions - you don’t start writing a custom physics engine.
10
u/BigFaceBass Jun 12 '19
When you first started learning JS, I bet that was confusing and overwhelming too. It was for me. That experience doesn't go away. As long as you practice programming, you will hit walls that you have to bust through.
The trick is to use that friction as an indication that:
Nobody knows everything; thus,
There's always something more to learn.
There are tons of tutorials, books, blogs, podcasts and forum threads from which to learn for free. If you don't mind dropping some money, take a class or sign up for Frontend Masters.
Don't be discouraged when you don't understand something. I was a musician 5 years ago when I decided on a career change. Now I'm a frontend dev at a large company and loving it. A few months ago, I was tasked with writing a parser for structured strings like formulas and I had zero clue of where to start. Even my coworkers who were CS grads were no help. So I bang my head against the wall for a week learning about compilers and lexers and stuff way beyond me but come away with a crappy, yet functional, parser that did the thing.
There's always another complex abstraction to tease apart. Enjoy it for the brain teaser that it is.
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.json is 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 called react, and then it downloads to your current working dir into a folder called node_modules.
If you poke around in node_modules/react, you'll see react has it's own package.json and node_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
5
u/Craycraft Jun 12 '19
There is a good class on Pluralsight called “Building a JavaScript Development Environment” by Cory House I found extremely helpful. It will cover what you’re frustrated with in full detail.
5
9
Jun 12 '19
JS is not straightforward.
Only beginners and extremely experienced people can think JS is straightforward and they are probably wrong.
6
u/ParkerZA Jun 12 '19
JS can be fucking weird at times, but that's usually coming from other languages.
3
4
Jun 12 '19 edited Jun 12 '19
The definitive guide for all things is the official documentation for each one. I'd focus on Node.js for servers, MySQL or MongoDB for databases, and learn React and Redux to handle state changes on web apps. Babel is to help transpile ES6 into ES5 so that you can write in ES6 (necessary for react) and still have it readable by all browsers, regardless if they're up to ES6 or not. If you dont know what packages are, you should look into npm, why it exists and what it solves. If you're not familiar with github you need to get on that too.
I'd look into taking some full stack tutorials on udemy. Also look into what are servers, databases, "MVC," and other computer sciencey concepts.
React's official documentaiton and tutorials are the best thing going. Redux documentation isnt great but as a developer you should always be heavily reliant on official documentation. There's other resources but when you have to look up or learn things things fast, docs are the way to go.
CodeCademy has react and redux and full stack tutorials. Udemy's a great place for full stack tutorials too. But again, as nice as it is to be guided step by step and learn insights from a tutor, were this a work environment (which Im assuming you're building up to) you have to be able to find answers to problems quickly so using documentation and bookmarking the hell out of it is your best bet.
4
u/HarmonicAscendant Jun 12 '19 edited Jun 12 '19
I have spent a long time thinking about this, and it needs to be broken down to be fully answered.
A large part of the problem is basically JS exploded as the biggest language without any official tools that were desperately needed, so everyone wrote their own. After that we were left with a huge number of choices we never wanted in the first place. Slowly the community has chosen and certain tools like Babel have become 'semi-official'... but there is no official source of info on this, it is just a bunch of opinions on forums that people go by and a few loose stats as to what is popular.
Some companies wanted backwards compatibility with crappy old browsers, so all the innovations were ham strung and we were left with a lot of cruft and junk to wade through. This is still going on with regard to the 3rd point.
JS never had a proper modules system until 2015, so people invented different ones. Once the old unofficial ones were baked into everyone's projects it was very hard to get people to break backwards compatibility and embrace the proper official one.
So as of today without a lot of wasted time are totally in the dark! It is a nightmare, you are going to have to trust someone, so it might as well be me :)
- Keep it simple! Only use ES6 modules and forget bundling unless you really need it. If you NEED backwards compatibility for work then the easiest thing is to run your code through the TypeScript compiler. You can compile any JS (or of course TS) into a lower version JS (es5 etc) with one command line. No 'build scripts'.
For a beginner it is confusing as to what Node and the browser are for exactly. Node can be used for back-end programming, or just for built tools/utilities that are separate from the browser. NPM is just the JS package manager.
Node can finally use ES6 modules, but it is cutting edge even today 4 years after they were made official. Hoops must still be jumped through!:
https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff
If you are using TypeScript I have a boiler plate starter project you could take a look at that tries to make things as simple as humanly possible:
https://github.com/David-Else/zen-typescript-starter-project
TypeScript is just JavaScript with added types and cool things. You could adapt this template for plain JS if you could be bothered.
Remember, whatever people tell you, you don't need ANYTHING EXTRA to program JS today in a modular and professional way other than a text editor and a browser. That is why it is so amazing, it seems like people love to make things impossible when they can be so simple. Every build tool and utility you add might make one aspect easier, but don't forget it can add another layer of complexity that can soon tie you up it knots.
3
u/True_Scorpio23 Jun 12 '19
Also the free code camp React exercises are a good way to code along while you learn new topics. I read the official docs and then tried to test myself with the fcc challenges.
3
u/Zeeesty Jun 12 '19
So advanced topics like scope and closure are programming generalities that hopefully your understanding of will grow. While the issues you bring up are about building software in an ecosystem. As others have said this is not specific to JavaScript. And yes, these libraries should have documentation, if the documentation sucks, don't use the library, try to find another one with better docs.
all in all this is something that comes with the territory. You're unlikely to come across a problem that hasn't been solved, but when you use someone else's solution you have to build around their decisions.
NPM has great docs on the package file, start there. If you want to understand how to build things from scratch, that comes from experience. The ecosystem is hard, dont be discouraged, just understand its another thing to learn and take it step by step
3
u/kaptan8181 Jun 12 '19
One step at a time, keep learning. There is so much to learn. Tutorials barely scratch the surface. Just cool your mind and read the docs! Even if you don't understand what you have read, ask on Reddit.
3
u/myusernameisunique1 Jun 12 '19
As much as I dislike them for the loss of control, you should use the cli tools that come with the frameworks, or rather, look for frameworks that come with cli tools to do the boilerplating for you.
@angular/cli is the best (in my opinion), but vue.js does a good job with it's cli and react also has a cli.
If you are looking for a general purpose boilerplating tool then something like yeoman can be really helpful for someone just trying to get started.
And then of course there are all the 'starter' projects you find on github with one or more features you might find useful
2
u/TotesMessenger Jun 12 '19
1
Jun 12 '19
I started with AngularJS and did tons of tutorials and didn't always get what role NodeJS was playing or what exactly was going on, but I pushed through. Then I started learning how to setup a MEAN stack (Mongo, Express, Angluar, Node) to try and understand what role a server/database versus client plays. Then (interspersed with a bunch of python Django and Flask learning), I dug into Meteor for a little while before realizing I'm too tied to their "ecosystem" and I wanted to extract my code from needing them. Then I dove into React and stacks that replace Angular with React (MERN) and after all of that and a couple years of picking a specific type of app I want to develop and trying it out in all of those technologies (don't just make to-do lists), I feel like I finally understand what you're trying to understand. I understand how it all just works together and how to deal with the errors that come up when building projects, etc.. All of that said, I don't think I can explain it to you in a paragraph. I think it takes diving in hardcore and not worrying about how long it's going to take you to learn it. Your brain will start to see the patterns and make the connections and it all builds upon itself
1
Jun 12 '19
Dont worry dude, everyone is going through what you are going through. Anyone who says they didn't is probably lying. Many of us still struggle with it.
Just remember to be patient. I like to sit back and listen to this every once in a while https://www.youtube.com/watch?v=ErvgV4P6Fzc
1
u/Headpuncher Jun 12 '19
NPM, FYI, is a recursive acronym; NPM is Not a Package Manager. It's a repository fishing line.
Jokes aside, one of the issues with web-dev is that everyone wants to be front-end, back-end, dev-ops, project manager, UX designer and any other role you can imagine. But in any (worthwhile) workplace, those roles are fulfilled professionally by different people, and you can pick up a bit from each as you gain experience, not all at once as the Internet would have you believe.
1
u/ForScale Jun 13 '19
Shouldn't it be like NPM = NPM Package Management = Nearly Perfect Management of Package Management
That'd be recursive.
1
1
u/jkuhl_prog Jun 12 '19
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?
Node is a runtime environment for JavaScript. It is the Chrome V8 engine, pulled out of the browser with an API (written in C I think) exposed that we can work with (which gives us things like the file system API.)
NPM is a package manager (Node Package Manager.) I don't know if you're familiar with other languages, but it's similar to Java's Maven or Python's Pip. A package is essentially a third party library that you can install into your project, like React or Vue. We require package management because of dependencies, a dependency is a library that another library needs in order to run. For example, React has a number of libraries that it uses, and many of those libraries have their own dependencies. Rather than trying to maintain and install all these dependencies by hand, NPM manages that for us. This is the purpose of package.json and package-lock.json (the second is auto generated by NPM and rarely needs to be touched.) They ensure all our packages have the dependencies we need.
Node is not required to run applications in the browser, but is frequently used to compile your code into something browsers better understand. Webpack is frequently used (and used under the hood for things like create-react-app and the Vue CLI) to compile your javascript from a module based system to one understood by the browser, as modules don't have full browser support yet. Or it is used to transpile Typescript into JavaScript, or JSX into React.CreateElement() calls, as the browser understands neither Typescript or JSX. However, if you have no need for a compilation step, then you don't need Node for your front end. Node, for front end work, is usually just a compilation step between your editing, and your stuff being previewed on a browser.
Is there a definitive guide for the above and things like Babel, Gulp, and the various dependencies I see listed in projects?
Unfortunately there isn't one, unless someone put one together, as Babel, Gulp, Webpack, etc, are all built and maintained by different groups of people so your best bet is to try to understand them one at a time with their own resources. With that said, Traversy Media (Brad Traversy on Udemy or Youtube) has many videos covering many of the different JavaScript tools out there, and he goes into depth with good explanations, so I'd check him out.
1
u/SentFromBelow Jun 13 '19
Just think of NodeJS as JavaScript that can do stuff with your file system. That's why so many build tools depend on it. You wouldn't ask your browser to create a minified javascript bundle or transpile some code with babel or import a module to use in a project. That's pretty much all there is to it (apart from reading the docs for whatever piece of "ecosystem" you're trying to incorporate). I'd recommend going through the Webpack docs and try writing some basic servers in node it'll all start to make sense pretty quickly.
1
u/osoese Jun 12 '19
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?
Not even close - in fact my favorite is building command line (console) apps with node.js
1
u/tunisia3507 Jun 12 '19
All the tools around it are necessary because of deficits in the language. You can't have a very straightforward language unless you build up overwhelmingly complicated tools around it. If your language is more complicated the tooling can be simpler.
1
0
u/XPTranquility Jun 12 '19
JavaScript is a language and one of the many tools of a Front End Developer. Take a front end web course to learn more about those tools or a full stack course for node.
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