2017-03-19 8 views
1

私のアプリケーションは正常に動作しますが、実稼働環境に展開するときはいつでも「Uncaught SyntaxError:Unexpected token <」というエラーが表示されます。エラーの原因を調べるたびに、予想されるjavascript /assets/js/index.jsの代わりにindex.htmlをロードするwebpackバンドルが原因です。Webpack: "Uncaught SyntaxError:Djangoの予期しないトークン<"

Webpackはindex.html以外のすべてのリクエストを無視していると推測しますが、どの設定でもデフォルトとして指し示す必要はありません。私はこの問題を解決する方法を知りたいし、エントリーポイントとして設定されているものをレンダリングするようにしたいと思います。

トレースバック:あなたが見ている場合

Uncaught SyntaxError: Unexpected token < 

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <meta charset="utf-8"> 
    <!--<title></title>--> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <script type="text/javascript" src="/static/jquery/dist/jquery.js"></script> 
    <script type="text/javascript" src="/static/materialize-css/dist/js/materialize.min.js"></script> 
    </head> 
    <body> 
     <div id="container"></div> 
     <script type="text/javascript" src="/static/assets/bundles/main-210eb139915d8d6abedf.js" ></script> 
    </body> 

webpack.config.js

//require our dependencies 
var path = require('path') 
var webpack = require('webpack') 
var BundleTracker = require('webpack-bundle-tracker') 

module.exports = { 
    //the base directory (absolute path) for resolving the entry option 
    context: __dirname, 
    //the entry point we created earlier. Note that './' means 
    //your current directory. You don't have to specify the extension now, 
    //because you will specify extensions later in the `resolve` section 
    entry: [ 
     './assets/js/index' 
    ],  
    output: { 
     path: path.resolve('./assets/bundles/'), 
     filename: "[name]-[hash].js" 
    }, 

    plugins: [ 
     //tells webpack where to store data about your bundles. 
     new webpack.NoErrorsPlugin(), // don't reload if there is an error 
     new BundleTracker({filename: './webpack-stats.json'}), 
     //makes jQuery available in every module 
     new webpack.ProvidePlugin({ 
      jQuery: 'jquery', 
      'window.jQuery': 'jquery' 
     }) 
    ], 

    module: { 
    loaders: [ 
     // we pass the output from babel loader to react-hot loader 
     { 
     test: [/\.js$/, /\.es6$/, /\.jsx?$/], 
     exclude: /node_modules/, 
     loaders: ['babel'], 
     }, 
     { test: /\.css$/, loader: "style-loader!css-loader" } 
    ] 
    }, 

    resolve: { 
     //tells webpack where to look for modules 
     modulesDirectories: ['node_modules'], 
     //extensions that should be used to resolve modules 
     extensions: ['', '.js', '.jsx'] 
    } 
} 
+0

完全にトレースバックを追加してください。 –

+0

index.htmlを含めるように更新しました。 webpackは 'Uncaught SyntaxError:Unexpected token <' – MysteryManners

答えて

2

は、直接ブラウザ上で直接JSまたはCSSファイルにアクセスしてください404エラーで、あなたはあります静的ファイルを正しく処理しません。 dj-staticを使用するか、メインのHTTPサーバー(ApacheまたはNginx)でファイルの要求を処理するstaticを構成できます。

webpackがjsファイルをJS環境にロードしようとしていますが、urlリクエストの結果、JS構文エラー(xml/html)を引き起こすhtmlファイル(おそらく404エラー)が発生します。

+0

を出力するだけなので、実際にはトレースバックはありません。JSとCSSファイルはブラウザから直接アクセスできます。静的なファイルに関する限り、whitenoise.pyプラグインはそれらを管理しています。 – MysteryManners

+0

要求を送信するURLと応答を確認するネットワークタブを確認してください –

+0

すべてのパスが期待通りです。すべての200。私の疑惑は、webpackがエントリポイントとして指定したファイルを見つけることができず、代わりにindex.htmlがデフォルトであるということです。 – MysteryManners

関連する問題