简体   繁体   中英

How to call function from other local file Node.js?

I tried like this from main.js

// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const parser = require('./parser.js');
parser.parseHash(url)

But got error that: parser.parseHash is not function

// parser.js file
function parseHash(url) {
    ...
}

在此处输入图像描述

Are you exporting parseHash function in parser.js file?

module.exports  = {
parseHash,
}
// parser.js
exports.parseHash = function (url) {
    ...
};

//main.js
const parseHash = require('./parser').parseHash;
parseHash(url)

You need to export the function like this:

// parser.js file
function parseHash(url) {
    ...
}

module.exports = {
    parseHash: parseHash
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM