How to fix function problems in npm package?

Viewed 88

I built a simple npm package for work with URL. it's working perfectly when you use it locally but when I published the package, I got many different errors that I am going to write it down and it would be perfect if you can help to solve those problems. package is available [here][1].

code:

var getUrl = function(){
    let originalUrl = window.location.href;
    return originalUrl;
}

var slashDivider = function(){
    let sd = getUrl().split('/');
    return sd;
}

let getProtocol = () => {
    let gp = location.protocol;
    return gp;
}

let domainName = () => {
    let dn = location.hostname;
    return dn;
}

let domainWithProtocol = () => {
    let dwp = location.origin;
    return dwp;
}

let sitePort = () => {
    let sp = location.port;
    return sp;
}

let searchQuery = () => {
    let sq = location.search;
    return sq;
}

let pathName = () => {
    let pn = location.pathname;
    return pn;
}

let hashAddress = () => {
    let hd = location.hash;
    return hd;
}

let StringIndexFinder = (nameOfString) => {
    let stringToArray = nameOfString.split('');
    let stringArrayLength = stringToArray.length;
    let startingIndex = getUrl().indexOf(nameOfString);
    let endIndex = parseInt(stringArrayLength) + parseInt(startingIndex) - 1;
    return "from index " + startingIndex + ", end index " + endIndex ;
} 


let isEncrypted = () => {
    if(getUrl().includes("https")){
        return true;
    } else {
        return false;
    }
}

module.exports = [getUrl, slashDivider, getProtocol, domainName, domainWithProtocol,sitePort,searchQuery, pathName,hashAddress, StringIndexFinder, isEncrypted];

1_ I installed the package by npm i ulio --save and then call package function like this:

const getUrl = require('ulio');

console.log(getUrl())

and I got this error:

Uncaught TypeError: getUrl is not a function

2_ I made a small change in the package, I deleted all the functions and only kept the first function which is the getUrl, and it worked well!!!(why?)

const getUrl = require('ulio');

console.log(getUrl()) //output: http://localhost:1234/

then I add the second function which is the slashDivider and the code become like below:

var getUrl = function(){
    let originalUrl = window.location.href;
    return originalUrl;
}

var slashDivider = function(){
    let sd = getUrl().split('/');
    return sd;
} 

module.exports = getUrl;
module.exports = slashDivider;

by the following code, I got a result of a second function as a result of the first function too.

const getUrl = require('ulio');
const slashDivider = require('ulio');

console.log(getUrl())
console.log(slashDivider())

3_ now I published full package(include all functions) and get this error while trying console.log(isEncrypted()) error:

Uncaught TypeError: isEncrypted is not a function

and it's the same for all functions. How can I solve those problems and how can I make this package better? [1]: https://www.npmjs.com/package/ulio

1 Answers

This is happening because module.exports is being exported as an array. The first item is getUrl. Use this instead:

const getUrl = require('ulio')[0];
console.log(getUrl())

Of course, this will not work unless using Browserify or Webpack, because window will not be defined.

A better way to do this is to use module.exports as an object, like this:

module.exports = {
  getUrl: getUrl
  ...
}

Then, you can just use this:

const { getUrl, someOtherFunction } = require('ulio');
console.log(getUrl())
console.log(someOtherFunction())

The following code:

const getUrl = require('ulio');
console.log(getUrl())

Does not work, because, require('ulio') returns an array, and arrays cannot be called as functions.

Related