Javascript 如何修复npm包中的功能问题?

Javascript 如何修复npm包中的功能问题?,javascript,npm,package.json,Javascript,Npm,Package.json,我为URL构建了一个简单的npm包。当您在本地使用它时,它工作得非常好,但是当我发布包时,我得到了许多不同的错误,我将把它们写下来,如果您能够帮助解决这些问题,它将是完美的。 套餐[此处][1]有售 代码: var getUrl=function(){ 让originalUrl=window.location.href; 返回原始值; } var slashDivider=函数(){ 设sd=getUrl().split('/'); 返回sd; } 让getProtocol=()=>{ 设gp=

我为URL构建了一个简单的npm包。当您在本地使用它时,它工作得非常好,但是当我发布包时,我得到了许多不同的错误,我将把它们写下来,如果您能够帮助解决这些问题,它将是完美的。 套餐[此处][1]有售

代码:

var getUrl=function(){
让originalUrl=window.location.href;
返回原始值;
}
var slashDivider=函数(){
设sd=getUrl().split('/');
返回sd;
}
让getProtocol=()=>{
设gp=location.protocol;
返回总成;
}
让域名=()=>{
设dn=location.hostname;
返回dn;
}
让domainWithProtocol=()=>{
设dwp=location.origin;
返回dwp;
}
让sitePort=()=>{
设sp=location.port;
返回sp;
}
让searchQuery=()=>{
设sq=location.search;
返回sq;
}
让路径名=()=>{
设pn=location.pathname;
返回pn;
}
让hashAddress=()=>{
设hd=location.hash;
返回hd;
}
让StringIndexFinder=(nameOfString)=>{
设stringToArray=nameOfString.split(“”);
设stringArrayLength=stringToArray.length;
让startingIndex=getUrl().indexOf(nameOfString);
让endIndex=parseInt(stringArrayLength)+parseInt(startingIndex)-1;
返回“从索引”+开始索引+”,结束索引”+结束索引;
} 
让我加密=()=>{
如果(getUrl()包括(“https”)){
返回true;
}否则{
返回false;
}
}
module.exports=[getUrl、slashDivider、getProtocol、domainName、domainWithProtocol、sitePort、searchQuery、pathName、hashAddress、StringIndexFinder、isEncrypted];
1_ui通过
npm I ulio--save
安装程序包,然后像这样调用程序包函数:

const getUrl=require('ulio');
console.log(getUrl())
我得到了这个错误:

Uncaught TypeError: getUrl is not a function
2_u我在包中做了一个小改动,我删除了所有的函数,只保留了第一个函数,即getUrl,它工作得很好!!!(为什么?)

const getUrl=require('ulio');
console.log(getUrl())//输出:http://localhost:1234/
然后我添加了第二个函数,即slashDivider,代码如下所示:

var getUrl=function(){
让originalUrl=window.location.href;
返回原始值;
}
var slashDivider=函数(){
设sd=getUrl().split('/');
返回sd;
} 
module.exports=getUrl;
module.exports=slashDivider;
通过下面的代码,我得到了第二个函数的结果,也是第一个函数的结果

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

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

3\现在我发布了完整的软件包(包括所有函数),并在尝试
console.log(isEncrypted())
错误:

所有函数都是一样的。 我怎样才能解决这些问题,怎样才能使这个软件包更好?
[1] :

发生这种情况是因为
模块.exports
正在作为数组导出。第一项是
getUrl
。改用这个:

const getUrl=require('ulio')[0];
console.log(getUrl())
当然,除非使用Browserify或Webpack,否则这将不起作用,因为不会定义
window

更好的方法是使用
module.exports
作为对象,如下所示:

module.exports={
getUrl:getUrl
...
}
然后,您可以使用以下命令:

const{getUrl,someOtherFunction}=require('ulio');
console.log(getUrl())
console.log(someOtherFunction())
以下代码:

const getUrl=require('ulio');
console.log(getUrl())
不起作用,因为,
require('ulio')
返回一个数组,而数组不能作为函数调用

Uncaught TypeError: isEncrypted is not a function