2017-07-18 36 views
0

我遇到了一個我認爲來自webpack的錯誤。那就是:ReferenceError:全局未定義在eval

index.js:9 Uncaught ReferenceError: global is not defined 
    at eval (index.js:9) 
    at Object.<anonymous> (bundle.js:2548) 
    at __webpack_require__ (bundle.js:622) 
    at fn (bundle.js:48) 
    at eval (client:1) 
    at Object.<anonymous> (bundle.js:2541) 
    at __webpack_require__ (bundle.js:622) 
    at bundle.js:668 
    at bundle.js:671 

我的WebPack是:

import webpack from 'webpack'; 
import merge from 'webpack-merge'; 
import path from 'path'; 
import isDev from 'isdev'; 
import { Dir } from './src/utils'; 

const TARGET = process.env.npm_lifecycle_event; 

let Config = { 
    entry: [ 
    'babel-polyfill', 
    'react-hot-loader/patch', 
    path.join(Dir.src, 'client.js'), 
    ], 
    output: { 
    path: path.join(Dir.public, 'build'), 
    filename: 'bundle.js', 
    }, 
    target: 'node', 
    resolve: { 
    modules: [Dir.src, 'node_modules'], 
    extensions: ['*', '.js', '.jsx', '.json'], 
    }, 
    module: { 
    rules: [ 
     { 
     test: /\.js?$/, 
     enforce: 'pre', 
     loader: 'eslint-loader', 
     exclude: /node_modules/, 
     include: Dir.src, 
     }, 
     { 
     test: /\.js?$/, 
     loader: 'babel-loader', 
     exclude: /node_modules/, 
     }, 
    ], 
    }, 
    plugins: [ 
    new webpack.optimize.OccurrenceOrderPlugin(), 
    new webpack.DefinePlugin({ 
     'process.env': { 
     NODE_ENV: JSON.stringify(process.env.NODE_ENV), 
     }, 
    }), 
    ], 
}; 

if (TARGET === 'build:prod' && !isDev) { 
    Config = merge(Config, { 
    bail: true, 
    devtool: 'source-map', 
    output: { publicPath: '/build/' }, 
    plugins: [ 
     new webpack.optimize.DedupePlugin(), 
     new webpack.optimize.UglifyJsPlugin({ 
     comments: false, 
     dropDebugger: true, 
     dropConsole: true, 
     compressor: { 
      warnings: false, 
     }, 
     }), 
    ], 
    }); 
} 

if (TARGET === 'server:dev' && isDev) { 
    Config = merge(Config, { 
    devtool: 'eval', 
    entry: ['webpack-hot-middleware/client'], 
    plugins: [ 
     new webpack.HotModuleReplacementPlugin(), 
     new webpack.NoEmitOnErrorsPlugin(), 
    ], 
    }); 
} 

const WebpackConfig = Config; 
export default WebpackConfig; 

此錯誤纔開始顯示出來,一旦我說什麼終極版提出了服務器端渲染。因此,我正在使用窗口.__ PRELOADED_STATE__在./src/utils/store.js中的商店水合,並且它也在index.ejs這是呈現給客戶端的文件。

這也是我.ba​​belrc如果有的話:

{ 
    "presets": ["es2015", "react", "stage-0"], 
    "env": { 
     "development": { 
      "plugins": ["react-hot-loader/babel"], 
     }, 
    }, 
    "plugins": [ 
     "babel-root-import" 
    ], 
} 

希望任何人都可以在這方面幫助 - 我沒有找到我的研究和試驗的解決方案。謝謝!

回答

0

問題是,我認爲,這target: 'node'在您的webpack.config.js。這基本上表明,Webpack可能會假定該軟件包將運行在類似節點的環境中,環境提供了類似於globalrequire的全局變量。除非另有說明,否則Webpack會採用瀏覽器環境並重寫global以指向window。您的配置會禁用此重寫。

您可以從您的配置中刪除target: 'node',或通過將node: {global: true}添加到您的配置對象來明確啓用global重寫。

+0

感謝那@Stefan - 你是對的。我說的唯一原因是因爲我有一個問題說'無法解析'fs'',並且提出的解決方案是'target:'node'':https://github.com/webpack-contrib/css-loader/問題/ 447 ...然後我得到了'全局未定義',所以我有一種錯誤循環,並且找不到任何東西來殺死所有它們...任何想法? – adambargh

+0

從邏輯上講,你需要獲得webpack來模擬彈出的所有東西。例如'fs':'node:{fs:'empty'}'。我不知道這是否是你想要做的正確解決方案。 –

+0

也就是'webpack.config.js'中的'node:{global:true,fs:'empty'}' –