2017-02-12 5 views
0

webpackを使用してexpress.jsアプリケーションを構築する際に問題が発生しています。私はnode.jsコアモジュール 'http'を正しく処理していないと思われます。webpackビルドでノードコアモジュールを正しく使用する

。具体的に:

{ 
    "name": "basic-express-example", 
    "version": "1.0.0", 
    "description": "description here", 
    "main": "index.js", 
    "author": "", 
    "license": "ISC", 
    "scripts": { 
    "build": "webpack -d" 
    }, 
    "dependencies": { 
    "babel-core": "^6.22.1", 
    "babel-loader": "^6.2.10", 
    "babel-preset-es2015": "^6.22.0", 
    "babel-preset-react": "^6.22.0", 
    "express": "^4.14.1", 
    "webpack": "^2.2.1" 
    } 
} 

マイwebpack.config.js

var webpack = require('webpack'); 
var path = require('path'); 

var BUILD_DIR = path.resolve(__dirname, 'build'); 
var APP_DIR = path.resolve(__dirname, 'app'); 

var config = { 
    entry: APP_DIR + '/index.jsx', 
    output: { 
    path: BUILD_DIR, 
    filename: 'index.js' 
    }, 
    module : { 
    loaders : [ 
     { 
     test : /\.jsx?/, 
     include : APP_DIR, 
     exclude : [ 
      /node_modules/, 
      /build/ 
      ], 
     loader : 'babel-loader' 
     } 
    ] 
    }, 
    node: { 
    fs: 'empty', 
    net: 'empty' 
    } 
}; 

module.exports = config; 

ように私のアプリフォルダ内のファイルのみを検索しますように私のpackage.jsonが見えますindex.jsx

import express from 'express'; 
const app = express(); 

私はnpm buildを使用してアプリケーションを構築し、node build/index.jsを使用したファイルを実行しようとした場合、私はどのように私はこの問題を解決するのです

undefined:31 
    __proto__: http.IncomingMessage.prototype 
          ^
TypeError: Cannot read property 'prototype' of undefined 

を言うタイプのエラーを取得し、すなわちどのように私は作るのですかhttpモジュールを利用できますか?

事前にお問い合わせいただきありがとうございます。

+0

var http = require( 'http'); in index.js – owaishanif786

+0

@ owaishanif786 webpackを構築した結果、index.jsを直接変更することはできません。 index.jsxに追加しようとしましたが、私はまだ同じエラーが発生しています。 – user1993047

答えて

0

IはWebPACKの設定ファイルに以下を追加することによって、ノードモジュールをバンドルしないことによってthis linkを使用してそれを解決:

var fs = require('fs'); 

    var nodeModules = {}; 
    fs.readdirSync('node_modules') 
    .filter(function(x) { 
     return ['.bin'].indexOf(x) === -1; 
    }) 
    .forEach(function(mod) { 
     nodeModules[mod] = 'commonjs ' + mod; 
    }); 
    ... 
    externals: nodeModules, 
    ... 

Iはまた、外部からのHTTPモジュールを提供するとしてindex.jsx束ねてそれを解決しようとしましたcommonjsモジュールは、httpインポートを提供する同じファイルで使用されます。さて、少し曖昧に聞こえる...とにかく、他のランタイムエラーが発生しました...

関連する問題