我正在尝试运行本地开发服务器,并且在构建代码时遇到babel错误。
当我运行“ npm start
”时出现错误:
找不到相对于目录的预设“ env”
如果我运行“ npm install --save babel-preset-env
”,则先前的错误消失,并且出现错误:
找不到相对于目录的预设“ stage-2”
如果我运行“ npm install --save babel-preset-stage-2
”,则会收到错误消息:
TypeError: Converting circular structure to JSON
at Object.stringify (<anonymous>)
at stringify (C:\Users\oysta\AppData\Roaming\npm\node_modules\babel-cli\node_modules\core-js\library\fn\json\stringify.js:4:26)
at compile (C:\Users\oysta\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-register\lib\node.js:105:42)
at loader (C:\Users\oysta\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-register\lib\node.js:144:14)
at Object.require.extensions.(anonymous function) [as .js] (C:\Users\oysta\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-register\lib\node.js:154:7)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
at Object.<anonymous> (C:\Users\oysta\AppData\Roaming\npm\node_modules\babel-cli\lib\_babel-node.js:154:22)
似乎在尝试加载某种缓存对象时收到此错误:
var cacheKey = (0, _stringify2.default)(opts) + ":" + babel.version;
在我的webpack.config.js的模块部分中,我有:
rules: [
{
test: /\.jsx?$/,
loader: 'babel-loader',
include: [
path.resolve(__dirname, '../src'),
],
query: {
// https://github.com/babel/babel-loader#options
cacheDirectory: isDebug,
// https://babeljs.io/docs/usage/options/
babelrc: false,
presets: [
// A Babel preset that can automatically determine the Babel plugins and polyfills
// https://github.com/babel/babel-preset-env
['env', {
targets: {
browsers: pkg.browserslist,
},
modules: false,
useBuiltIns: false,
debug: false,
}],
// Experimental ECMAScript proposals
// https://babeljs.io/docs/plugins/#presets-stage-x-experimental-presets-
'stage-2',
// JSX, Flow
// https://github.com/babel/babel/tree/master/packages/babel-preset-react
'react',
// Optimize React code for the production build
// https://github.com/thejameskyle/babel-react-optimize
...isDebug ? [] : ['react-optimize'],
],
plugins: [
// Adds component stack to warning messages
// https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-react-jsx-source
...isDebug ? ['transform-react-jsx-source'] : [],
// Adds __self attribute to JSX which React will use for some warnings
// https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-react-jsx-self
...isDebug ? ['transform-react-jsx-self'] : [],
],
},