Symlinking与npm链接反应模块为本地开发提供了错误

在我的React npm模块的开发过程中,我将它与npm link 。 完成此操作后,程序包会正确链接,并显示在消费者应用程序node_modules

该模块公开一个接口来创build一个React组件。 因为我正在使用Reactjsxes2015 ,所以我使用babel在预发布阶段使用npm prepublish hook来传输我的模块代码。

但是,当我尝试使用webpack构build我的消费者应用程序时(即,在链接它之后),在我的包中出现错误,指出:

模块构build失败:错误:无法find预设“es2015”

现在有趣的是,如果我在npm上发布软件包,那么npm install从我的消费者应用程序的registry中npm install ,构build它,一切工作正常。

我尝试在我的消费者应用程序中安装babel-preset-es2015 ,但是却开始抱怨没有findbabel

找不到模块:错误:无法parsing模块'babel'

再次,如果我从npmregistry安装它一切正常,在构build过程中不会引发错误。

我也尝试安装babel-corebabel-clibabel-polyfill ,都无济于事。

我在任何地方都使用babel v6.1.x ,并意识到所有最近的变化,但是我想我错过了一些明显的东西,如果有人能够帮助我,我会很感激它,因为不断发布模块来testing东西的作品只是不好的做法。

为了完整起见,这里是代码:

  • 消费者应用

以下是链接模块的步骤:

  1. cd ~/Sites/me/react-leafbox
  2. npm link
  3. cd ~/Sites/me/mapp
  4. npm link react-leafbox
  5. npm start

build筑后堆积痕迹:

 ERROR in ../react-leafbox/lib/index.js Module build failed: Error: Couldn't find preset "es2015" at OptionManager.mergePresets (/Users/daniel/Sites/me/mapp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:329:17) at OptionManager.mergeOptions (/Users/daniel/Sites/me/mapp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:289:12) at OptionManager.addConfig (/Users/daniel/Sites/me/mapp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:223:10) at OptionManager.findConfigs (/Users/daniel/Sites/me/mapp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:366:16) at OptionManager.init (/Users/daniel/Sites/me/mapp/node_modules/babel-core/lib/transformation/file/options/option-manager.js:410:12) at File.initOptions (/Users/daniel/Sites/me/mapp/node_modules/babel-core/lib/transformation/file/index.js:191:75) at new File (/Users/daniel/Sites/me/mapp/node_modules/babel-core/lib/transformation/file/index.js:122:22) at Pipeline.transform (/Users/daniel/Sites/me/mapp/node_modules/babel-core/lib/transformation/pipeline.js:42:16) at transpile (/Users/daniel/Sites/me/mapp/node_modules/babel-loader/index.js:14:22) at Object.module.exports (/Users/daniel/Sites/me/mapp/node_modules/babel-loader/index.js:83:14) @ ./src/js/components/App.jsx 2:10-34 

添加额外的babel相关的依赖关系(我认为这不应该是必要的,因为它们在react-leafbox模块中可用)之后的堆栈跟踪:

 ERROR in ../react-leafbox/lib/index.js Module not found: Error: Cannot resolve module 'babel' in /Users/daniel/Sites/me/react-leafbox/lib @ ../react-leafbox/lib/index.js 8:11-39 

我在MAC OSX El Capitan v10.11.1上使用node v5.0.0 npm v3.3.6上的MAC OSX El Capitan v10.11.1 。 我也尝试使用node v4.2.1 npm 2.14.7 ,这给了我相同的错误。

我find了解决我的问题。 我在webpack 文档中发现了这个:

重要提示:这里的装载机是相对于它们所应用的资源来parsing的。 这意味着它们不会相对于configuration文件被parsing。 如果你使用npm安装了加载器,并且你的node_modules文件夹不在所有源文件的父文件夹中,那么webpack无法find加载器。 您需要将node_modules文件夹添加为resolveLoader.root选项的绝对path。 (resolveLoader:{root:path.join(__ dirname,“node_modules”)})

webpack.config.js添加root选项之后,关于无法parsingbabel的错误消失了。 相反,我得到一个新的说它不能解决react 。 我把它定义为一个对等的依赖关系,这让我觉得它需要在本地找不到的时候回退,然后我在文档中find了这个:

resolve.fallback一个目录(或绝对path目录),webpack应该查找在resolve.root或resolve.modulesDirectories中找不到的模块。

我结合这两个选项在我的消费者应用程序的webpack.config.js ,它的工作!

 // webpack.config.js var path = require('path'); module.exports = { entry: path.resolve(__dirname, 'src/js/index.js'), output: { path: path.resolve(__dirname, 'dist'), filename: "build.js" }, module: { loaders: [ { test: /\.jsx?$/, exclude: /(node_modules)/, loader: 'babel', query: { presets:[ 'react', 'es2015' ] } } ] }, resolve: { extensions: [ '', '.js', '.jsx' ], fallback: path.join(__dirname, "node_modules") }, resolveLoader: { root: path.join(__dirname, "node_modules") } }; 

我希望这可以帮助任何人遇到类似的问题。

更新Webpack ^ 2.5.0

删除resolveLoader对象,并将resolveLoader设置为false来replaceresolveLoader

 resolve: { extensions: [ '', '.js', '.jsx' ], symlinks: false } 

也许这只是拼写错误,但应该是babel-preset-es2015而不是babel-preset-2015。 npm安装babel-preset-es2015。

我有这个问题,事实certificate,我已经安装在全球,当我需要安装到我的应用程序与npm install webpack webpack-dev-server --save-dev

我试图设置路由器演示时遇到了这个问题: https : //github.com/reactjs/react-router-tutorial/tree/master/lessons/01-setting-up

这里的所有解决scheme都不适合我,但是添加了一个.babelrc文件作为内容:

{“presets”:[“react”]}