2016-09-02 45 views
2

我试图用browserify使用google-trends-api npm包,但我无处可去。这是我的main.js文件google-trends-api npm没有找到使用browserify的模块

var googleTrends = require('google-trends-api'); 

var options = { 
    geo: 'country name', 
    date: 'yyyymm', 
    keywords: ['some', 'list', 'of', 'keywords'], 
    category: 'some category' 
}; 

googleTrends.apiMethod(options) 
.then(function(results){ 
    console.log("Here are your google trend results!", results); 
}) 
.catch(function(err){ 
    console.log("there was an error :(", err); 
}); 

这里是我的bundle.js

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ 
var googleTrends = require('google-trends-api'); 

// var options = { 
//  geo: 'country name', 
//  date: 'yyyymm', 
//  keywords: ['some', 'list', 'of', 'keywords'], 
//  category: 'some category' 
// }; 

// googleTrends.apiMethod(options) 
// .then(function(results){ 
//  console.log("Here are your google trend results!", results); 
// }) 
// .catch(function(err){ 
//  console.log("there was an error :(", err); 
// }); 
},{"google-trends-api":2}],2:[function(require,module,exports){ 
(function (__dirname){ 
'use strict'; 

module.exports = require(__dirname + '/lib/utils/'); 

}).call(this,"/node_modules/google-trends-api") 
},{}]},{},[1]); 

这里是我得到的错误:

bundle.js:1 Uncaught Error: Cannot find module '/node_modules/google-trends-api/lib/utils/index.js' 

不知道什么可以诚实地难住了。我在Mac上,并且消息出现在浏览器的控制台中。

回答

2

google-trends-api模块包含require调用,包括表达式:

module.exports = require(__dirname + '/lib/utils/'); 

这表达引起问题为Browserify,因为它不分析所述require并且不包括进一步的依赖关系。

然而,无视的是,google-trends-api模块具有至少一个dependency不是在浏览器中使用兼容的,所以即使你是解决表达-IN-require -calls问题,它不会工作在浏览器。

+0

更多想法如何使用它在客户端。 – Lakshya