2016-08-24 47 views
0

這裏是我的pacjage.json:爲什麼的WebPack不能找到裝載機

{ 
    "name": "redux-todo", 
    "version": "1.0.0", 
    "description": "", 
    "main": "app.js", 
    "scripts": { 
    "start": "webpack-dev-server" 
    }, 

    "devDependencies": { 
    "babel": "^6.5.2", 
    "babel-loader": "^6.2.5", 
    "babel-preset-es2015": "^6.13.2", 
    "babel-preset-react": "^6.11.1", 
     "webpack": "^1.13.2" 
    }, 
    "dependencies": { 
    "react": "^15.3.1", 
    "react-dom": "^15.3.1", 
    "react-redux": "^4.4.5", 
    "redux": "^3.5.2" 
    } 
} 

webpack.config.js:

var path = require('path'); 

module.exports = { 
entry: './index.js', 
output: { 
    path: './', 
    filename: 'app.js' 
}, 
devServer: { 
    inline: true, 
    port: 3334 
}, 
resolveLoader: { root: path.join(__dirname, "node_modules") }, 
module: { 
    loaders: [ 
     { 
      test: /\.js$/, 
      exclude: '/node_modules', 
      loader: 'babel', 
      query: { 
       presets: ['es2015', 'react'] 
      } 
     } 
    ] 
} 
}; 

和我有以下項目的目錄結構:

├── actions.js 
├── components 
├── containers 
├── index.js 
├── node_modules 
├── package.json 
├── reducers.js 
├── test.js 
└── webpack.config.js 

項目目錄的絕對路徑爲/home/dmitriy/WebstormProjects/Redux-todo

那麼爲什麼當我運行npm start它崩潰,出現錯誤:

ERROR in (webpack)/~/process/browser.js Module build failed: Error: Couldn't find preset "es2015" relative to directory "/usr/local/lib/node_modules/webpack/node_modules/process"

這是什麼/usr/local/lib/node_modules/webpack/node_modules/process路徑,以及爲什麼它說,它搜索相關呢?

谷歌搜索這個錯誤我發現,

IMPORTANT: The loaders here are resolved relative to the resource which they are applied to. This means they are not resolved relative the the configuration file. If you have loaders installed from npm and your node_modules folder is not in a parent folder of all source files, webpack cannot find the loader. You need to add the node_modules folder as absolute path to the resolveLoader.root option. (resolveLoader: { root: path.join(__dirname, "node_modules") })

應該修復它,但你可以看到我有它在我的配置,仍然看到此錯誤。

我在Ubuntu 16.04 LTS,版本的NodeJS是4.2.6,3.5.2 NPM

+0

在父目錄中是否有任何.babelrc文件? – nitte93user3232918

+0

不,沒有.babelrc – dKab

回答

0

你只排除/node_modules爲絕對路徑:如果你想遞歸

exclude: '/node_modules' 

排除所有node_modules嘗試使用:

exclude: /node_modules/ 

的區別是微妙的,但前者是作爲使用帶有絕對路徑node_modules,後者是一個與node_modules匹配的任何路徑的正則表達式。

這應該沒有resolveLoader配置工作。所以,你可以刪除這個字段:

resolveLoader: { root: path.join(__dirname, "node_modules") }, 
+0

我看不出如何排除節點模塊與webpack無法解析預設的事實有關 - 正如您可以在配置的最終版本中看到的,我仍然沒有結尾斜槓在node_modules之後,它仍然有效。 – dKab

+0

因爲不排除'/ usr/local/lib/node_modules/webpack/node_modules/process'。它不在您的項目文件夾中(在您的項目'node_modules'內)。它可能安裝在全球。你的babel loader甚至不應該處理這些文件:)。我更喜歡使用'include'而不是'exclude'。它更明確,更不容易出錯。 – dreyescat

0

其實我刪除node_modules,扭捏的package.json一點點:

{ 
    "name": "redux-todo", 
    "version": "1.0.0", 
    "description": "", 
    "main": "app.js", 
    "scripts": { 
    "start": "webpack-dev-server" 
    }, 
"dependencies": { 
    "babel": "^6.5.2", 
    "react": "^15.3.1", 
    "react-dom": "^15.3.1", 
    "react-redux": "^4.4.5", 
    "redux": "^3.5.2" 
    }, 
    "devDependencies": { 
    "babel-core": "^6.13.2", 
    "babel-loader": "^6.2.5", 
    "babel-preset-es2015": "^6.13.2", 
    "babel-preset-react": "^6.11.1", 
    "webpack": "^1.13.2", 
    "webpack-dev-server": "^1.14.1" 
} 
} 

AAAND它的工作。不知道爲什麼。下面是的WebPack配置看起來像現在:

var path = require('path'); 

module.exports = { 
entry: './index.js', 
output: { 
    path: './', 
    filename: 'app.js' 
}, 
devServer: { 
    inline: true, 
    port: 3334 
}, 
module: { 
    loaders: [ 
     { 
      test: /\.js$/, 
      exclude: '/node_modules', 
      loader: 'babel', 
      query: { 
       presets: ['es2015', 'react'] 
      } 
     } 
    ] 
} 
}; 

我不知道那是什麼有關_(ツ)_ /¯

0

我的項目裝載機配置是這樣的:

{ 
    test: /\.js$/, 
    exclude: '/node_modules', 
    loader: "babel-loader" 
} 

而且.babelrc文件包含此:

{ 
    "presets": ["es2015","react"] 
} 

觀望的WebPack t時的第一配置您發佈的帽子,我注意到選項resolveLoader: { root: path.join(__dirname, "node_modules") }。如果將所有加載程序放在正確的路徑上(項目根目錄下的node_modules目錄),則不需要此選項。

相關問題