I'm currently working on an electron application, I'm trying to use node in the renderer, but whenever I try to use require, it throws ReferenceError: require is not defined, I have nodeIntegration set to true and I'm not entirely sure what I could do to make it work.
I have tried setting contextIsolation to false but that breaks jQuery.
This is what I'm trying to reference in the js file:
const electron = require('electron');
Which just results in ReferenceError: require is not defined as I said above.
Related
A quick problem I faced while developing a web-app using SvelteKit (and by extension, Vite):
Inside the <script> tag of my +page.svelte file I tried defining an empty placeholder File object the following way:
let formObject: FormCreationData = {
fileToUpload: new File([], ''),
anotherField: "",
...
};
While it should work in normal JS/TS (and Svelte if you aren't using SvelteKit), it now throws the following error:
ReferenceError: File is not defined
at +page.svelte:13:14
Why is this the case?
Since SvelteKit implements Server-Side Rendering (SSR) - The code that is on the +page.svelte file has to run both on client browsers and the Vite server.
The File class is only available in browsers, so it won't be able to fulfill this requirement (You might know that Node.js offers the fs module in order to allow for file operations instead).
This means that there are two ways to possibly fix this problem:
Disable SSR using the variable ssr in the +page.ts/js file:
export const ssr = false;
Find a way to define the File object at a point where the code runs on the browser (This can be done by checking the browser variable under the $app/environment module, or inside of one of the supported Svelte hooks, such as onMount()).
I am trying to create a local browser-based application.
I need to have access to some dlls so I used a module called ffi-napi:
Example (what I need to use is not specifically the libm module, just simplifying the example):
var ffi = require('ffi-napi')
var libm = ffi.Library('libm', {
'ceil': ['double', ['double']]
})
libm.ceil(1.5)
This works fine when on I am running on Electron. But when I compile it for use with the browser, I am getting this error message (which is being thrown by Webpack/node-gyp-build:
Error in mounted hook: "ReferenceError: require is not defined"
I understand that require is not native on the browser mode, but the framework I am using, I believe, already has browserify integrated (I am using Quasar framework). I think so because the other local modules I can use require() on the browser just fine.
Is there any way I can use ffi-napi on the browser?
Help!
When importing protrator in an Angualr application I try to use the function browser.waitForAngularEnabled.
When I run the code I get the error:
E/launcher - Error: TypeError: Cannot read property 'waitForAngularEnabled' of undefined
I am importing the browser in this way
const { browser } = require('protractor');
When I click through to the protractor module in VS code I can see the browser object in there but in runtime this error is thrown.
In Protractor, you are provided with a global browser object. So you don't need to require it, the browser object is accessible without requiring it.
Solution:
Just remove the require and it will work.
Note:
You have to import it only if you are working with typescript.
I am switching my desktop app from electron to nw.js because of source code security features.
I am requiring the module jquery-confirm in my nodejs file, I get this error:
Uncaught Error: jquery-confirm requires jQuery
I fix this error by:
var $ = require('jquery');
window.jQuery = $;
var jconfirm = require('jquery-confirm');
and importing the js file in index.html like this:
<script>require('./bot.js')</script>
Becuase I get the same jquery error if I require the js file like:
<script src="bot.js"></script>
When I fix the error like above and I launch the app with nw.js, it immediately crashes giving this error:
TypeError: $(...).button is not a function
First question:
I am assuming there is something wrong with jquery. When I import it in index.html, it doesnt work at all. However, I am still running into issues after requiring it in the js file. How can I fix this?
Second question:
Why dont I get the Uncaught Error: jquery-confirm requires jQuery error if I import my js file using 'require' instead of ?
This is an issue of javascript context in Node Webkit. We must ensure all the javascript required inside the browser (the window context) is brought into that context by using the src attribute of a script tag.
Node Webkit places JavaScript from require() statements in the nodejs global context - and makes it unavailable inside the browser.
So we see errors such as $ is not defined or $(...).button() is not a function if files are in opposite contexts.
I am trying to build a web-desktop application using ElectronJs and AngularJS, so I wrote my code and everything worked fine when I start my desktop application, but in the browser, I have a problem - I have an undefined method.
Here is the line where I am having a problem :
const electron = require('electron')
Everything works fine as I said when I tap my command :
electron .
But when I open my file index.html in my browser I get this error in my console
ReferenceError: require is not defined
I tried some solutions like importing 'require.js' but nothing is working.
<script src="require.js"></script>
But I get another error which is :
Error: Module name "electron" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded
You can wrap the require calls in an if block and check if require is defined:
if (typeof require === 'function') {
const electron = require('electron')
}
You don't want to be requiring electron (or any other Node modules) if you're running in a browser anyway.