这是我的目录结构:
├───demo
│ ├───entry
│ │ ├───index.js
│ │ ├───tap.js
│ │ └───util.js
│ ├───node_modules
│ ├───index.html
│ ├───package.json
│ └───webpack.config.js
├───src
│ ├───tap.js
│ └───util.js
├───index.js
└───package.json
在demo/entry/index.js
我有
import tap from '../../src/tap';
编译时,babel会抛出错误
ERROR in ../src/tap.js
Module build failed: Error: Cannot find module 'core-js/library/fn/get-iterator'
但如果我像这样导入
,它就有效import tap from './tap';
文件./tap.js
和../../src/tap
相同。
问题是node_modules
吗?因为在demo
目录中,babel和其他任何内容都在node_modules
中,所以我可以import
我喜欢的任何文件。但src/tap.js
没有node_modules
这样的父目录或兄弟目录,因此会触发此错误。
答案 0 :(得分:2)
尝试将babel-loader和babel-core安装为dev-dependency:
npm install babel-core babel-loader --save-dev
您还需要更新.babelrc
{
"presets" : ["es2015", "react"]
}
对我有用。祝你好运;)
答案 1 :(得分:0)
也许你没有安装core-js。 我曾经遇到过这个问题。
yarn install
答案 2 :(得分:0)
更新为节点版本11.10.0
(当前版本)对我有用。
答案 3 :(得分:-2)
我刚安装了core-js,它就起作用了。