为什么我的脚本与节点一起正确运行但不能与webpack / babel一起使用?

时间:2016-08-29 18:49:50

标签: javascript node.js webpack babeljs

我不确定我应该问这个问题的方式。请随时纠正我,这样我就可以做得更好。

我有以下脚本来调用GA分析API:

var google = require ("googleapis");
var key = require ('./client_id.json');
const VIEW_ID = 'ga:80820965';

let jwtClient = new google.auth.JWT(
  key.client_email, 
  null,
  key.private_key,
  ['https://www.googleapis.com/auth/analytics.readonly'],
  null
);

    jwtClient.authorize(function (err, tokens) {
      if (err) {
        console.log(err);
        return;
      }
      let analytics = google.analytics('v3');
      queryData(analytics);
    });

var queryData = function(analytics) {
      analytics.data.ga.get({
        'auth': jwtClient,
        'ids': VIEW_ID,
        'metrics': 'ga:uniquePageviews',
        'dimensions': 'ga:pagePath',
        'start-date': '30daysAgo',
        'end-date': 'yesterday',
        'sort': '-ga:uniquePageviews',
        'max-results': 10,
      }, function (err, response) {
        if (err) {
          console.log(err);
          return;
        }
        console.log(JSON.stringify(response, null, 4));
      });  
    }

module.exports = {
    queryData
};

当我独立运行脚本时,脚本运行正常,就像node garoute.js一样。但是,当我按照npm run dev运行整个项目时,我发现了以下错误:

[1]   analytics.data.ga.get({
[1]            ^
[1] 
[1] TypeError: Cannot read property 'data' of undefined 

为什么会出现此错误?这与ES5 / ES6问题有关吗?

这是我的webpack配置:

module.exports = {
  entry: './public/client.js',
  output: {
    path: './public',
    filename: 'bundle.js'       
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        query: {
          plugins: ['transform-decorators-legacy']
        }
      }
    ]
  },
  resolve: {
    extensions: ['', '.js', '.json'] 
  },
node: {
  fs: "empty",
  net: "empty",
  tls: "empty",
}
};

和我的包裹:

{
  "name": "dashboard-react-dynamodb",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "webpack-watch": "webpack -w",
    "express-server": "node ./server",
    "dev": "concurrently --kill-others \"npm run webpack-watch\" \"npm run express-server\"",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/simonbreton/dashboard-react-dynamodb.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/simonbreton/dashboard-react-dynamodb/issues"
  },
  "homepage": "https://github.com/simonbreton/dashboard-react-dynamodb#readme",
  "dependencies": {
    "axios": "^0.13.1",
    "babel-core": "^6.14.0",
    "babel-loader": "^6.2.4",
    "babel-plugin-transform-class-properties": "^6.10.2",
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-plugin-transform-runtime": "^6.9.0",
    "babel-polyfill": "^6.9.1",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.5.0",
    "babel-preset-stage-0": "^6.5.0",
    "babel-register": "^6.9.0",
    "d3": "^4.2.2",
    "es6-promise": "^3.2.1",
    "express": "^4.14.0",
    "ga-api": "0.0.4",
    "google-auth-library": "^0.9.8",
    "googleapis": "^12.4.0",
    "isomorphic-fetch": "^2.2.1",
    "lodash": "^4.15.0",
    "react": "^15.1.0",
    "react-dom": "^15.1.0",
    "react-json-table": "^0.1.1",
    "react-redux": "^4.4.5",
    "redux": "^3.5.2",
    "redux-logger": "^2.6.1",
    "redux-thunk": "^2.1.0",
    "simple-oauth2": "^0.8.0",
    "webpack": "^1.13.1"
  },
  "devDependencies": {
    "babel-plugin-transform-strict-mode": "^6.11.3",
    "babel-preset-es2016": "^6.11.0",
    "babel-preset-stage-1": "^6.13.0",
    "concurrently": "^2.1.0",
    "redux-devtools-log-monitor": "^1.0.11"
  }
}

0 个答案:

没有答案