2017-10-12 95 views
2

我有一個React項目已經使用babel-preset-es2015,轉移到babel-preset-env模塊構建失敗。在React和Webpack項目中從「babel-preset-es2015」轉換爲「babel-preset-env」時的問題

出現此錯誤消息:

ERROR in ./src/index.js 
Module build failed: Error: Couldn't find preset "es2015" relative to directory "/path/to/project" 
    at /path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19 
    at Array.map (<anonymous>) 
    at OptionManager.resolvePresets (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20) 
    at OptionManager.mergePresets (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10) 
    at OptionManager.mergeOptions (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14) 
    at OptionManager.init (/path/to/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12) 
    at File.initOptions (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:212:65) 
    at new File (/path/to/project/node_modules/babel-core/lib/transformation/file/index.js:135:24) 
    at Pipeline.transform (/path/to/project/node_modules/babel-core/lib/transformation/pipeline.js:46:16) 
    at transpile (/path/to/project/node_modules/babel-loader/lib/index.js:50:20) 
    at Object.module.exports (/path/to/project/node_modules/babel-loader/lib/index.js:175:20) 

下面是我的WebPack配置:

WORKS(用巴別預置-2015)

module.exports = { 
    ... 
    module: { 
    rules: [ 
     { 
     test: /\.js$/, 
     exclude: /node_modules/, 
     use: { 
      loader: 'babel-loader', 
      options: { 
      presets: [ 
       'es2015', 
       'react', 
       'stage-1'] 
      } 
     } 
     } 
    ] 
    }, 
    ... 
}; 

不起作用( with babel-preset-env)

module.exports = { 
    ... 
    module: { 
    rules: [ 
     { 
     test: /\.js$/, 
     exclude: /node_modules/, 
     use: { 
      loader: 'babel-loader', 
      options: { 
      presets: [ 
       'env', 
       'react', 
       'stage-1'] 
      } 
     } 
     } 
    ] 
    }, 
    ... 
}; 

的package.json依賴關係:

"babel-core": "^6.26.0", 
"babel-loader": "^7.1.2", 
"babel-preset-env": "^1.6.0" 
+0

這是奇怪的。您是否嘗試刪除'node_modules'並執行新的'npm install'? – Oblosys

+0

您是否更新過.babelrc文件? – Jaxx

+0

@Oblosys我有,它沒有幫助。 –

回答

1

所以我增加了一個.babelrc文件和移動的裝載程序選項那裏,事情似乎現在的工作就好了。仍然不確定爲什麼它不能在webpack模塊中工作。

的WebPack

module.exports = { 
    ... 
    module: { 
    rules: [ 
     { 
     test: /\.js$/, 
     exclude: /node_modules/, 
     use: 'babel-loader' 
     } 
    ] 
    }, 
    ... 
}; 

.babelrc

{ 
    "presets": [ 
    "env", 
    "react", 
    "stage-1" 
    ] 
}