Client on node: Uncaught ReferenceError: require is not defined

前端 未结 8 1472
遥遥无期
遥遥无期 2020-11-21 07:20

So, I am writing an application with the node/express + jade combo.

I have client.js, which is loaded on the client. In that file I have code that calls

8条回答
  •  别跟我提以往
    2020-11-21 07:56

    I am coming from an electron environment, where I need IPC communication between a renderer process and the main process. The renderer process sits in an HTML file between script tags and generates the same error. The line

    const {ipcRenderer} = require('electron')
    

    throws the Uncaught ReferenceError: require is not defined

    I was able to work around that by specifying node integration as true when the browser window (where this HTML file is embedded) was originally created in the main process.

    function createAddItemWindow() {
    //Create new window
    addItemWindown = new BrowserWindow({
        width: 300,
        height: 200,
        title: 'Add Item',
    
        //The lines below solved the issue
        webPreferences: {
            nodeIntegration: true
        }
    })}
    

    That solved the issue for me. The solution was proposed here. Hopes this helps someone else. Cheers.

提交回复
热议问题