We are using guacamole-common-js in one of our projects and using guacamole-common.min.js in the script results in a module not found error:
<script type="text/javascript" src="guacamole-common.min.js"></script>
I am using guacamole-common.js: 1.4.0-a
At first, I thought this might be related to guacamole-common.js version but I tried downgrading its version but still facing the same error.
The use of module.exports indicates that the file is a CommonJS module. This is the default module format of Node.js and is not supported by browsers.
Either:
You have a module designed for Node.js and not web browsers. There might be a browser targetted version available.
You have a module designed for use with a module bundling tool (such as Webpack or Parcel) and should use one of those to build your browser-side JS application.
Try carefully reading the documentation from wherever you downloaded the JS file from.
Related
I apologize, my lack of knowledge of how to build modern Javascript apps is showing.
We have a Capacitor app that uses plain Javascript, without any build tools. This works fine. We're trying to add Microsoft Code Push support, via https://github.com/mapiacompany/capacitor-codepush, and we're running into a problem with how to integrate it into our app.
For Capacitor and its plugins, we use tags to include the plugin.js files from the various node_modules/.../dist directories.
If we do this with node_modules/capacitor-codepush/dist/plugin.js, we get an error about missing acquisitionSdk. Including node_modules/code-push/script/acquisition-sdk.js doesn't help.
Ok, so maybe there are a bunch of dependencies? We tried using rollup to see if we could get that to work, but cannot. Using this simple input file:
import { codePush } from 'capacitor-codepush';
console.log("hello");
we get [!] Error: Invalid value "iife" for option "output.format" - UMD and IIFE output formats are not supported for code-splitting builds.
Any help would be appreciated.
The only way I was able to get it working was to switch to using webpack. For reasons I don't understand, my initial attempts using rollup did not work. webpack was the key.
You should run the dist version of your app. Meaning, you should use a bundler like webpack. Unbundled (pure) code can't use these features.
I have written a node module and published it as a node package. It works when I use it in backend applications (pure nodejs, no babel or transpile).
However, when I use this same npm module in the frontend (in my case a 'create-react-app') application, it breaks. Bellow is the exact error:
Module parse failed: Unexpected token (14:2)
You may need an appropriate loader to handle this file type.
The error is referring to my use of the spread operator (...). I would prefer not to have to rewrite the library, and would rather add some kind of transpiler to package my library. I haven't found a clear solution to this, they are all very convoluted.
I have tried using rollupjs, and https://github.com/insin/nwb. Neither sadly seem to be what I'm after.
Run my code:
You can install the library to your create react app using npm i cbs-proxy-client#0.0.3. And then add the line const cbsProxyClient = require('cbs-proxy-client') or import cbsProxyClient from 'cbs-proxy-client' to any of your scripts.
Advice would be appreciated :)
A library used for frontend is expected to package an already transpiled version of the source javascript. To do this, you might want to use rollup as a build process in your library to create a bundle file. You can use babel to transpile your code for desired browser support. Let's say the bundle file is saved in dist/bundle.js. Now you will modify the package.json to load this bundled file as the entry file using main parameter in package.json
If you are building using rollup or webpack, it is easy to miss that the bundled file should be prepared as a library. This means that importing the file using commonjs should be able to export correct variables. A typical webpack build removes such exports because it is supposed to work straight on a browser. This blog is in my bookmarks titled "js library steps" since I was creating my first js library.
Note that you do not need to put your generated file in version control. You can use npm files property in package.json to package your bundled files while ignoring them in git.
Just want to know is it possible to initialize/start node.js from C++ application. Root of this question is that I am having a C++ console application, which launches an javascript application which does require('os').Now It is failing as I suppose node.js is missing or not initialized as I am getting Uncaught ReferenceError: require is not defined
Edit 1:
I tried browserify and it resolved the require() issue. Now problem is that I am not able to use any of the methods exposed by the required module.
I am getting "undefined is not a function" error.
Since require is not defined it seems like your JavaScript engine is not supporting the CommonJS specification. You cannot run node modules without CommonJS support.
Generally it's hard to run node modules anywhere but in node, because of the node internals. There exists software to transform node modules and scripts to other platforms like webpack and browserify. Maybe those will create JavaScript that you can use in your project.
I am running a polymer starter kit app and I need a firebase connection, but I don't know why the browser say that require is undefined.
$ npm install firebase --save
var Firebase = require("firebase");
The problem is that require is a function provided by node.js's 'module' module. It is not part of the ES5 spec, and so it is not natively available in web browsers.
In order to load modules using require, you will have to use a web bundler (like Webpack, Browserify or RequireJS). This will bundle together all of the JS in your project into a single file, automatically handling calls to require.
I'd recommend reading Getting started with Webpack to get you on your way.
I need to use this library banking.js which uses modules. I have downloaded RequireJS and I have been reading for hours, but I can't seem to find any tutorial that uses simply require('Module') like how it is suggested on the github page, they all use different kind of methods that pass anonymous functions (they didn't work for me either).
I constantly get the following error:
Uncaught Error: Module name "banking" has not been loaded yet for context: _. Use require([])
I followed the documentation but nothing is working.
NodeJS projects use the CommonJS (CJS) format for modules; RequireJS is mostly used for AMD modules with a few hacks to try to make the two interoperable.
You may want to look into Browserify; it will bundle CJS modules into a browser-includeable JS file, and provides some replacements for NodeJS core modules.