【问题标题】:Why does my Babel import not work?为什么我的 Babel 导入不起作用?
【发布时间】:2018-05-27 13:41:15
【问题描述】:

我的模块.js

function cube(x) {
  return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
export { cube, foo };

app.js

import React from 'react';
import ReactDOM from 'react-dom';
import {cube} from './my-module';

console.log(cube(2)); 没有错误,但显示,import 似乎不起作用。 stackoverflow,不需要更多细节,代码已经表明了我的意思,请不要强迫我们添加更多细节。

"ReferenceError: cube is not defined
    at eval (eval at 35 (http://localhost:8080/build/bundle.js:84:1), <anonymous>:1:1)
    at Object.35 (http://localhost:8080/build/bundle.js:84:1)
    at __webpack_require__ (http://localhost:8080/build/bundle.js:20:30)
    at Object.34 (http://localhost:8080/build/bundle.js:71:18)
    at __webpack_require__ (http://localhost:8080/build/bundle.js:20:30)
    at http://localhost:8080/build/bundle.js:63:18
    at http://localhost:8080/build/bundle.js:66:10"

webpack.config.js

module: {
    loaders: [
      {
        test: /\.js$/,
        loaders: ['babel-loader'],
        exclude: /(node_modules)/
      },

.babelrc

{
  "presets": [
    "es2015",
    "react",
    "stage-0"
  ],
  "plugins": [
    "transform-react-jsx"
  ],
  "retainLines": true,
  "sourceMaps": "both",
  "ignore": [
    "*.css"
  ]
}

【问题讨论】:

  • babelrc 不是bablerc
  • 也是它的import { cube } from './my-module'
  • @azium,嗨,import React from 'react' 行也显示 React 未定义。 `

标签: javascript webpack babeljs


【解决方案1】:

试试import { cube } from './my-module'

当您执行import cube 时,您正在寻找默认导出

【讨论】:

  • 仍然无法正常工作,import React from 'react' 行也显示 React 未定义。
  • 你是对的,Chrome浏览器说React 未定义,实际上它已定义,看来即使我在import {cube} from './my-module';行添加断点,我们也不能使用chrome控制台看看React 是什么。
【解决方案2】:

我尝试使用您提供的信息重现错误,但我不能。

具体来说,我创建了这个简单的项目,一切都按预期工作。

package.json

{
  "name": "dummy-react",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-0": "^6.24.1",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "webpack": "^3.10.0"
  }
}

.babelrc

{
  "presets": [
    "es2015",
    "react",
    "stage-0"
  ],
  "plugins": [
    "transform-react-jsx"
  ],
  "retainLines": true,
  "sourceMaps": "both",
  "ignore": [
    "*.css"
  ]
}

webpack.config.js

var webpack = require('webpack')

module.exports = {
  entry: './app.js',
  output: {
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loaders: ['babel-loader'],
        exclude: /(node_modules)/
      }
   ]
  }
}

my-module.js

function cube(x) {
  return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
export { cube, foo };

app.js

import React from 'react'
import ReactDOM from 'react-dom'
import {cube} from './my-module'

console.log(cube(2))

ReactDOM.render(
  <h1>Hello, World!</h1>,
  document.getElementById('root')
);

index.html

<html>
  <head>
  </head>
  <body>
     <div id="root"></div>
     <script src="./bundle.js"></script>
  </body>
</html>

所以问题可能不在您提供的代码中。

【讨论】:

    猜你喜欢
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2016-10-12
    • 2011-10-29
    • 2014-07-21
    • 2023-01-30
    • 1970-01-01
    相关资源
    最近更新 更多