当前在将Material-UI从v0迁移到v3的过程中,我面临着多个文件从错误位置导入依赖项的问题。
我们以@babel/runtime
依赖性为例。
以下是我的package.json和yarn.lock文件的摘录:
"dependencies": {
"@material-ui/core": "3.0.0",
"@material-ui/icons": "3.0.0",
"react-swipeable-views": "0.12.16",
"react-swipeable-views-utils": "0.12.16",
},
"devDependencies": {
"babel-cli": "6.26.0",
"babel-core": "6.26.3",
"babel-loader": "7.1.5",
"babel-runtime": "6.26.0",
"babel-regenerator-runtime": "6.5.0",
},
"@material-ui/core@3.0.0":
version "3.0.0"
dependencies:
"@babel/runtime" "7.0.0-rc.1"
"@material-ui/icons@3.0.0":
version "3.0.0"
dependencies:
"@babel/runtime" "7.0.0-rc.1"
react-event-listener@^0.6.0, react-event-listener@^0.6.2:
version "0.6.3"
dependencies:
"@babel/runtime" "7.0.0-rc.1"
react-swipeable-views-core@^0.12.16:
version "0.12.16"
dependencies:
"@babel/runtime" "7.0.0-beta.42"
recompose@^0.28.2:
version "0.28.2"
dependencies:
"@babel/runtime" "7.0.0-beta.56"
@babel/runtime@7.0.0-beta.42":
version "7.0.0-beta.42"
dependencies:
core-js "^2.5.3"
regenerator-runtime "^0.11.1"
"@babel/runtime@7.0.0-beta.56":
version "7.0.0-beta.56"
dependencies:
regenerator-runtime "^0.12.0"
"@babel/runtime@7.0.0-rc.1":
version "7.0.0-rc.1"
dependencies:
regenerator-runtime "^0.12.0"
也共享我的webpack配置的一部分,因为我有一种直觉,这可能是这里的罪魁祸首:
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: "babel-loader",
query: {
presets: [["es2015", {loose: true, modules: false}], "react", "stage-0"],
plugins: ["transform-decorators-legacy"],
cacheDirectory: true,
},
},
],
entry: [
"babel-regenerator-runtime",
path.resolve(__dirname, sourcePath, `${jsFilename}.js`),
],
还有我的babelrc:
{
"presets": [
"es2015",
"react",
"stage-0",
"flow"
],
"plugins": [
"transform-decorators-legacy",
["transform-runtime", {
"polyfill": false,
"regenerator": true
}],
["babel-plugin-root-import", {
"rootPathSuffix": "src"
}]
]
}
我所有的webpack错误都指向getPrototypeOf
目录中缺少的@babel/runtime/helpers
文件:
ERROR in ./node_modules/@material-ui/core/styles/withTheme.js
Module not found: Error: Can't resolve '@babel/runtime/helpers/getPrototypeOf' in '/project/node_modules/@material-ui/core/styles'
@ ./node_modules/@material-ui/core/styles/withTheme.js 20:46-94
@ ./node_modules/@material-ui/core/styles/index.js
@ ./src/components/Root.js
@ ./src/app.js
@ multi babel-regenerator-runtime ./src/app.js
ERROR in ./node_modules/react-event-listener/dist/react-event-listener.cjs.js
Module not found: Error: Can't resolve '@babel/runtime/helpers/getPrototypeOf' in '/project/node_modules/react-event-listener/dist'
@ ./node_modules/react-event-listener/dist/react-event-listener.cjs.js 10:38-86
@ ./node_modules/@material-ui/core/Popover/Popover.js
@ ./node_modules/@material-ui/core/Popover/index.js
@ ./src/components/Header/AnonymousMenu.js
@ ./src/components/Header/index.js
@ ./src/components/ListeningRoute.js
@ ./src/components/Root.js
@ ./src/app.js
@ multi babel-regenerator-runtime ./src/app.js
如果我在node_modules中搜索该文件,除根目录以外的所有目录都包含该文件:
$ for dir in $(find node_modules -name 'runtime'); do ls $dir/helpers/getPrototypeOf.js; done
node_modules/recompose/node_modules/@babel/runtime/helpers/getPrototypeOf.js
node_modules/@material-ui/icons/node_modules/@babel/runtime/helpers/getPrototypeOf.js
node_modules/@material-ui/core/node_modules/@babel/runtime/helpers/getPrototypeOf.js
node_modules/react-event-listener/node_modules/@babel/runtime/helpers/getPrototypeOf.js
ls: cannot access 'node_modules/@babel/runtime/helpers/getPrototypeOf.js': No such file or directory
节点模块应该递归搜索依赖项,因此我们可以在不中断项目的情况下并发使用多个版本的软件包,但是在这种情况下,它们实际上是在根node_modules/
目录中搜索它们。
其他困惑来自以下事实:我们仍在项目中使用babel v6,但@babel
已安装在project/node_modules
中。
有人可以解释为什么发生此行为,如果这是仅纱线问题,以及如何解决此问题?还是可能与webpack有关?
我想在一个过渡期内拥有并发版本的Material-ui v0和v3,但是当前的解决方案是硬迁移到v3(以及将babel从v6迁移到v7)。这确实阻碍了我们的迁移。
注意:cp node_modules/@material-ui/core/node_modules/@babel/runtime/helpers/getPrototypeOf.js node_modules/@babel/runtime/helpers
删除了所有Webpack错误...
答案 0 :(得分:2)
在webpack中,有一个名为resolve的选项。您将必须设置resolve选项,以便@ material-ui将使用其自己的node_modules
。当前,@ material-ui使用通过npm或yarn安装的最上层node_modules
来导入依赖项。
在您的webpack配置中,包括
module.exports = {
...
resolve : {
modules : ['node_modules']
},
...
}
这样做,webpack将在相对目录中查看node_modules
。