I understand a similar question has been asked before. I am trying to import a payment gateway module (paynow/nodeJS) into a web application. Browser console is displaying
"ReferenceError: require is not defined"
at line
const { Paynow } = require("paynow");
What is that I could be doing wrong?
There are two likely causes of this:
You are trying to run the JS in a browser instead of in Node.js
You have Node.js configured to use ES6 modules instead of CommonJS modules
If the former, run the code designed to be run in Node.js in Node.js.
If the latter, either use import instead or configure the system to use CommonJS modules (don't use the .mjs file extension and don't use package.json's type field.
i'm in trouble of setting Webstorm for MEAN app
To be honest, this is my first meeting with JS and I can't understand how to correctly get syntax highlight and IDE help
What should I do after these steps?:
Installed Webstorm
Installed Node.js
Installed Mongo DB
Now I’m really confused because of the need to install Node.js packages, #types, ECMA standarts, javascript libraries, babel library
Because...
IDE asks me to use export {something} but when I'm running it it doesn't work,
AND when I use module.exports = function f, other modules doesn't see my function, but it compiles!
Also, exports without module. doesn't work.
Am I really should install all of these things or there any other more simple ways?
In my Koa project, I use the koa-static to serve the static files. And the simple project it just goes as following:
var koa = require('koa');
var serve = require('koa-static');
var app = new koa();
app.use(serve('./public'));
app.listen(3000);
We plan to put all the static files in the public folder.
But I get the following error message when I want to run the Node app.
koa-static#4.0.1#koa-static\index.js:39
return async function serve (ctx, next) {
^^^^^^^^
SyntaxError: Unexpected token function
So my Node version is 6.11.0. And the koa-static used async/await function, which is supported by Node.js newer than v7.6.0.
So if I don't plan to update the Node, is there anyway to work around this issue? I manually use Babel to transpile my ES6 code. But for the package, can I babel it?
The thing is that Koa is specifically built around async/await and Node.js 7.6.0 or higher, which is mentioned at GitHub. So package developers are just following the basic standard and most of the time do not bother supporting earlier versions of Node.js. Maybe you should reconsider migrating?
If absolutely not, there are two visible ways to make it work:
Setup package.json step to give you transpile with Babel as part of installation process (npm install has preinstall and postinstall steps). That way you will have transpiled packages right after installation.
Configure your Webpack (Gulp, Grunt) to preprocess node_modules or specifically koa-static with Babel. Performance degradation could be significant, and this is rather lazy solution.
I've been trying to use a npm module called solc in AngularJs but I can't seem to succeed in anyway.
I know it's meant to be used with a Node application but I was wondering if there is a way to use it client side.
I've been trying to use Require.js and require every file included inside the git-repo but there are too many dependencies so I was wondering if there is another way.
I've been looking at browserify but that doesn't seems to do exactly what I need.
Does anyone have any idea?
You can use a npm package in browser applications if the package does not have any dependency on Node's built-in APIs. That's because Node APIs are available on server side and not on browser.
If you see here and here in the github repo of solc, you will see that it requires Node's built-in modules path and https. So it will not work in any browser application.
I've encountered several JavaScript projects and libraries that use this require() function, in order to include other files, like this:
require('somefile')
I never heard of that, and apparently it's something of node.js, which I don't have and don't use.
I just intend to use these JavaScript libraries in my own websites, but I'm seeing all sorts of instructions involving "npm" (whatever that may be). Then there is supposedly a replacement for that called required.js, but that seems to use a different syntax, like forcing to use require([...]) or something whereas the projects I need to include just do require(...).
What is the easiest way to deal with this require(...) stuff when using JavaScript projects in regular html5 websites? (i.e. all supposed to run client side)
Addition: I already tried require.js, but it doesn't seem to 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'm getting this error:
Uncaught exception: Error: Module name "assert" has not been loaded
yet for context: _. Use require([])
And this happens with anything that contains require()
Another addition: I noticed people mentioning 'browserify'. And some of the js projects I'm trying to include also recommend this. Apparently this is supposed to generate one single ready to use .js file that I can Include. But
Why don't they just publish this browserified .js directly? Is there a reason why I need to compile it myself? it's supposed to be something universal for all browsers or websites, right?
This browserify thing, which is apparently to avoid node.js, actually seems to require node.js itself (the instructions all mention "npm -g install browserify" etc)
Libraries should ideally support the following, depending on its environment. Lets say you are using a library called "MyLib.js".
No module loader detected
window.MyLib
Requirejs detected
define(['MyLib'], function (MyLib) {
// Do something
return {};
});
CommonJS detected, like node or use of browserify or bower
var MyLib = require('MyLib');
Not all libs conform to this, but they should. Maybe the lib you were looking at only supports Node. Looking at the source of jQuery you see something like this:
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 );
}
Node.js is now used a lot to manage JavaScript projects, even if the project is client side. For example, Grunt, Bower, Browserify, Gulp and plenty other build tools run on Node.js, even though you can use them on client-side projects. Using those tools doesn't make your project depend on Node in production. Node is only used for development. To install those tools, one uses npm, which is a package manager. Like Maven or Ivy, npm will install packages and their dependencies by downloading them from the internet.
Libraries which install instructions involve npm are meant to be used in Node, but may be used in the browser after they have been converted with Browserify. Download the library with npm, then convert it to browser style using Browserify (which you install using npm, because itself runs on Node). You should get a single JavaScript file that you can import in your client-side project.
Libraries that are especially targeted for the browser will often refer to Bower as the install method instead of npm. Bower is also a package manager, but it is designed to download and install library written for browsers, not Node. If a library you want is available on Bower, you are able to download it and all its dependencies with bower install <lib>. Bower will put all files in a bower_components folder in the current directory. You can then copy those files to your project, or make your project import them directly from this folder.
So, browserify is simply a tool which makes it possible to use node-style modules in the browser. Yes, you do need to have node.js installed in order to use npm and browserify. But these times you need node.js for the most of your frontend toolsets.
npm is full of modules that are written in JavaScript and also run in the browser. With browserify you can use these modules in the browser.
It works by shimming the whole require mechanism and making it work in the browser. This also means that you can organize your code in modules:
// add.js
module.exports = function(x, y) {
return x + y;
}
// app.js
var add = require('./add.js');
var result = add(7, 8);
Now you could generate your bundle (the only script you need to include in your html) by just running browserify app.js -o bundle.js.
If you don't like the browserify approach you can also use the --standalone option to generate a JavaScript file in the UMD format. You could then simply include this in your html and use it with window.add for the previous example.
The require statement is handled by require.js, which can be used standalone in a javascript environment. It is a module loader which optimises loading dependencies in the browser. There is a Node.js implementation for it, but that doesn't mean that you have to use Node, you can just include require in your project.
(p.s: npm is Node Package Manager, and would be unnecessary for your project unless you're using Node. It streamlines the inclusion of javascript node modules into your project.)