我的最终目标是在使用 Browserify 和 Babel 7 的项目中使用 Yarn Workspaces。这是我遇到的问题的最小再现。基本上,似乎子文件夹中存在 package.json 文件(这是您在使用 Yarn Workspaces 时拥有的东西之一)破坏了我的 Browserify 构建,我不知道为什么。
首先,安装依赖项(可以使用yarn或者npm,没关系):
$ npm install
然后确认 Browserify+Babel 构建工作:
$ npm run build
> browserify-babelify-yarn-workspaces@1.0.0 build /home/user/projects/browserify-babelify-yarn-workspaces
> browserify a/index.js -t babelify --outfile bundle.js
耶,一切都好!我编译的代码在 bundle.js 中。
现在让我们在a
文件夹中创建一个虚拟的 package.json:
$ echo "{}" > a/package.json
那不应该改变构建,对吧?错误的:
$ npm run build
> browserify-babelify-yarn-workspaces@1.0.0 build /home/user/projects/browserify-babelify-yarn-workspaces
> browserify a/index.js -t babelify --outfile bundle.js
/home/user/projects/browserify-babelify-yarn-workspaces/a/index.js:1
import lib from "./lib.js";
^
ParseError: 'import' and 'export' may appear only with 'sourceType: module'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! browserify-babelify-yarn-workspaces@1.0.0 build: `browserify a/index.js -t babelify --outfile bundle.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the browserify-babelify-yarn-workspaces@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! /home/user/.npm/_logs/2018-11-16T15_58_43_540Z-debug.log
我无法弄清楚为什么会这样。任何帮助,将不胜感激!