YEETter 23414323424
YEETter 23414323424

Reputation: 57

How can I import another JS file (which is in the same directory) in a electron JS file

I'd like to import "new.js" inside "main.js", using const new = require("new.js"), i tried also import "./new.js", but I doesn't want to work.

This is my code if you want to see it

// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow() {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: true
    }
  })

  // and load the index.html of the app.
  mainWindow.loadFile('index.htm')

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
  createWindow()

  app.on('activate', function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
//password

i hope you can help me, please do NOT judge my coding skills, i am still learning. Anyway here there is my html and css code too.

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
  <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  <meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
  <link rel="stylesheet" href="index.css">
  <link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
  <title>FYco</title>
</head>

<body>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <br>
  <h1>FYco app</h1>
  <form id="whoAreyou">
    <label for="whoAreyou">
      Chi sei?
    </label>
    <br>
    <select>
      <option value="1">Federico Santucci</option>
      <option value="2">Yuri de Marco</option>
      <option value="3">Simone Stroppiana</option>
      <option value="4">Edoardo Casali</option>
      <option value="5">Guido Betti</option>
    </select>
    <br>
    <label for="password">Password</label>
    <br>
    <div id="password">
      <input id="password" type="password">
    </div>
    <form id="send">
      <input id="send" type="submit">
    </form>
  </form>
  <!-- You can also require other files to run in this process -->
  <script src="./renderer.js"></script>
  <script src="new.js"></script>
</body>

</html>
html, body {
    margin: 0;
    padding: 0;
    background-color: #333;
}

select {
    text-align: center;
    border: 3px solid #555;
    width: 25em; height: 3em;
    font-family: 'Lato', sans-serif;
    font-style: italic;
}

h1 {
    color: #f1f1f1;
    font-size: 48px;
    font-family: 'Lato', sans-serif;
    text-align: center;
}

label {
    text-align: center;
    color: #f1f1f1;
    font-size: 48px;
    font-family: 'Lato', sans-serif;
    font-style: italic;
}

input[type=password] {
    border: 3px solid #555;
    width: 25em; height: 3em;
}

input[type=submit] {
    border: 3px solid #555;
    width: 25em; height: 3em;
}
/* IDs */
#password {text-align: center}
#whoAreyou{text-align: center}
/*
font-family: 'Lato', sans-serif;
*/

I hope everything of my code is clear to you.

Upvotes: 1

Views: 3174

Answers (1)

Brad Harker
Brad Harker

Reputation: 120

We need to see the content of your new.js file and how you're exporting the module first.

Ensure you're exporting the content you need using module.exports

For example

new.js

const newObj = { a: 5, b:6 };
module.exports = newObj;

main.js

const importedObj = require('./new.js');
console.log(importedObj);
// will output { a: 5, b:6 }

Upvotes: 1

Related Questions