Getting started with Node.jsnpmWeb Apps With ExpressFilesystem I/OExporting and Consuming ModulesExporting and Importing Module in node.jsInstalling Node.jsMySQL integrationReadlinepackage.jsonEvent EmittersAutoreload on changesEnvironmentCallback to PromiseExecuting files or commands with Child ProcessesCluster ModuleException handlingKeep a node application constantly runningUninstalling Node.jsnvm - Node Version ManagerhttpUsing StreamsDeploying Node.js applications in productionSecuring Node.js applicationsMongoose Libraryasync.jsFile uploadSocket.io communicationMongodb integrationHandling POST request in Node.jsSimple REST based CRUD APITemplate frameworksNode.js Architecture & Inner WorkingsDebugging Node.js applicationNode server without frameworkNode.JS with ES6Interacting with ConsoleCassandra IntegrationCreating API's with Node.jsGraceful ShutdownUsing IISNode to host Node.js Web Apps in IISCLINodeJS FrameworksgruntUsing WebSocket's with Node.JSmetalsmithParsing command line argumentsClient-server communicationNode.js Design FundamentalConnect to MongodbPerformance challengesSend Web NotificationRemote Debugging in Node.JSMysql Connection PoolDatabase (MongoDB with Mongoose)Good coding styleRestful API Design: Best PracticesDeliver HTML or any other sort of fileTCP SocketsHackBluebird PromisesAsync/AwaitKoa Framework v2Unit testing frameworksECMAScript 2015 (ES6) with Node.jsRouting ajax requests with Express.JSSending a file stream to clientNodeJS with RedisUsing Browserfiy to resolve 'required' error with browsersNode.JS and MongoDB.Passport integrationDependency InjectionNodeJS Beginner GuideUse Cases of Node.jsSequelize.jsPostgreSQL integrationHow modules are loadedNode.js with OracleSynchronous vs Asynchronous programming in nodejsNode.js Error ManagementNode.js v6 New Features and ImprovementEventloopNodejs Historypassport.jsAsynchronous programmingNode.js code for STDIN and STDOUT without using any libraryMongoDB Integration for Node.js/Express.jsLodashcsv parser in node jsLoopback - REST Based connectorRunning node.js as a serviceNode.js with CORSGetting started with Nodes profilingNode.js PerformanceYarn Package ManagerOAuth 2.0Node JS LocalizationDeploying Node.js application without downtime.Node.js (express.js) with angular.js Sample codeNodeJs RoutingCreating a Node.js Library that Supports Both Promises and Error-First CallbacksMSSQL IntergrationProject StructureAvoid callback hellArduino communication with nodeJsN-APIMultithreadingWindows authentication under node.jsRequire()Route-Controller-Service structure for ExpressJSPush notifications

Using Browserfiy to resolve 'required' error with browsers

Other topics

Example - file.js

In this example we have a file called file.js.

Let's assume that you have to parse an URL using JavaScript and NodeJS querystring module.

To accomplish this all you have to do is to insert the following statement in your file:

const querystring = require('querystring'); 
var ref = querystring.parse("foo=bar&abc=xyz&abc=123");

What is this snippet doing?

Well, first, we create a querystring module which provides utilities for parsing and formatting URL query strings. It can be accessed using:

const querystring = require('querystring'); 

Then, we parse a URL using the .parse() method. It parses a URL query string (str) into a collection of key and value pairs.

For example, the query string 'foo=bar&abc=xyz&abc=123' is parsed into:

{  foo: 'bar',  abc: ['xyz', '123']   }

Unfortunately, Browsers don't have the require method defined, but Node.js does.

Install Browserfy

With Browserify you can write code that uses require in the same way that you would use it in Node. So, how do you solve this? It's simple.

  1. First install node, which ships with npm. Then do:

npm install -g browserify

  1. Change into the directory in which your file.js is and Install our querystring module with npm:

npm install querystring

Note: If you don't change in the specific directory the command will fail because it can't find the file which contains the module.

  1. Now recursively bundle up all the required modules starting at file.js into a single file called bundle.js (or whatever you like to name it) with the browserify command:

browserify file.js -o bundle.js

Browserify parses the Abstract Syntax Tree for require() calls to traverse the entire dependency graph of your

  1. FinallyDrop a single tag into your html and you're done!

<script src="bundle.js"></script>

What happens is that you get a combination of your old .js file (file.js that is) and your newly created bundle.js file. Those two files are merged into one single file.

Important

Please keep in mind that if you want to make any changes to your file.js and will not affect the behaviour of your program. Your changes will only take effect if you edit the newly created bundle.js

What does that mean?

This means that if you want to edit file.js for any reasons, the changes will not have any effects. You really have to edit bundle.js since it is a merge of bundle.js and file.js.

Contributors

Topic Id: 7123

Example Ids: 23890

This site is not affiliated with any of the contributors.