The javascript library uses require (), but don't I have or is nodeJS used?

I came across several JavaScript projects and libraries that use this require () function to include other files, for example:

require('somefile')

I have never heard of this, and apparently this is something like node.js, which I do not have and do not use.

I just intend to use these JavaScript libraries on my own sites, but I see all kinds of instructions involving "npm" (whatever that is). Then there is supposedly a replacement for required.js , but it seems that a different syntax is used, for example, the forced use of require ([...]) or something, while the projects I need to include just require (...).

What is the easiest way to handle this, do you need (...) material when using JavaScript projects on regular html5 websites? (i.e. everyone should work on the client side)


Addition: I already tried require.js, but it does not work. For example, the first line in somelibrary.js is this:

var assert = require('assert')

when I previously included require.js and then somelibrary.js, I get this error:

Unhandled exception: Error: module name "assert" not loaded but for context: _. Use require ([])

And this happens in everything that requires () contains


Another addition: I noticed that people mention the "browser". And some of the js projects that I'm trying to include also recommend this. Apparently, this should create one ready-to-use .js file that I can include. But

  • .js ? , ? - -, ?

  • , , -, node.js, , , node.js( "npm -g install browserify" ..)

    /li >
+4
4

, . , "MyLib.js".

window.MyLib

define(['MyLib'], function (MyLib) {
  // Do something
  return {};
});

CommonJS, node

var MyLib = require('MyLib');

, . , , , Node. jQuery, - :

if ( typeof module === "object" && typeof module.exports === "object" ) {
    // For CommonJS and CommonJS-like environments where a proper window is present,
    // execute the factory and get jQuery
    // For environments that do not inherently posses a window with a document
    // (such as Node.js), expose a jQuery-making factory as module.exports
    // This accentuates the need for the creation of a real window
    // e.g. var jQuery = require("jquery")(window);
    // See ticket #14549 for more info
    module.exports = global.document ?
        factory( global, true ) :
        function( w ) {
            if ( !w.document ) {
                throw new Error( "jQuery requires a window with a document" );
            }
            return factory( w );
        };
} else {
    factory( global );
}
+1

Node.js JavaScript, . , Grunt, Bower, Browserify, Gulp Node.js, . , Node . Node . , npm, . Maven Ivy, npm , .

, , npm, Node, Browserify. npm, Browserify ( npm, Node). JavaScript, .

, , Bower npm. Bower , , , Node. Bower, bower install <lib>. Bower bower_components . .

+1

, - , node - . , npm node.js. node.js frontend.

npm , JavaScript, . .

, require . , :

// add.js
module.exports = function(x, y) {
    return x + y;
}

// app.js
var add = require('./add.js');
var result = add(7, 8);

( script, html), browserify app.js -o bundle.js.

, --standalone , JavaScript UMD. html window.add .

+1

require require.js, javascript. -, . Node.js, , Node, .

(ps: npm Node , , Node. javascript node .)

0

All Articles