Webpack + Babel:找不到相对于目录的预设 "es2015"

Webpack + Babel: Couldn't find preset "es2015" relative to directory

我有一个使用 Webpack 和 Babel 的 React 项目。当我在办公电脑上创建它时,Webpack 运行 很好。当我将项目克隆到我的个人计算机上时,出现以下错误:

ERROR in ./react_minesweeper.jsx
Module build failed: Error: Couldn't find preset "es2015" relative to directory "/Users/louisstephancruz/Desktop"
at /Users/louisstephancruz/Desktop/w6d5/minesweeper/node_modules/babel-core/lib/transformation/file/options/option-manager.js:298:19
at Array.map (native)
at OptionManager.resolvePresets (/Users/louisstephancruz/Desktop/w6d5/minesweeper/node_modules/babel-core/lib/transformation/file/options/option-manager.js:269:20)

这是我的 webpack.config.js:

module.exports = {
  entry: './react_minesweeper.jsx',
  output: {
    path: './',
    filename: 'bundle.js',
  },
  module: {
    loaders: [
      {
        test: [/\.jsx?$/, /\.js?$/],
        exclude: /(node_modules)/,
        loader: 'babel',
        query: {
          presets: ['es2015', 'react']
        }
      }
    ]
  },
  devtool: 'source-map',
  resolve: {
    extensions: ['', '.js', '.jsx' ]
  }
};

这是我的 package.json:

{
  "name": "minesweeper",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack-dev-server --inline"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.16.0",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.16.2"
  },
  "dependencies": {
    "react": "^15.3.2",
    "react-dom": "^15.3.2"
  }
}

我的node版本是:v5.6.0 我的 npm 版本是:3.6.0

npm install babel-preset-es2015

有帮助吗?

npm inpm install

应该在你的 package.json 依赖项和开发依赖项中安装所有包(只要你的 NODE_ENV 环境变量不等于 production)。


要检查您是否安装了特定的软件包,您可以这样做:

npm ls babel-preset-es2015

如果由于某种原因您的 NODE_ENVproduction 并且您想安装开发依赖项,您可以使用:

npm install --only=dev

相反,以下内容可用于处理已构建代码且不需要访问任何开发依赖项的生产机器:

npm install --only=prod


我建议在您的存储库的根目录中创建一个包含以下内容的 .babelrc

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


对于 webpack 配置,您可能需要指定一些其他选项:

{ context: __dirname
, resolve: { root: __dirname, extensions: [ '.js', '.jsx', '.json' ] }
}

除了其余的配置之外,这还会告诉 webpack 捆绑的根目录应该从哪里开始,以及哪些文件扩展名应该被视为模块(哪些扩展名可以在 require / import 语句)。

我建议查看 webpack 的 resolve.extensions 以获取有关该位的更多信息。

我从“/Users/username”目录中删除 .babelrc(隐藏)文件后解决了这个问题。

npm install babel-preset-es2015 babel-preset-stage-2 --save

对我有用。

对我来说,我必须全局安装这个包。

npm install babel-preset-es2015 --save -g