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(立方体(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"
]
}
答案 0 :(得分:0)
尝试import { cube } from './my-module'
执行import cube
时,您正在寻找默认导出
答案 1 :(得分:0)
我尝试使用您提供的信息重现错误但我不能。
具体来说,我创建了这个简单的项目,一切都按预期工作。
<强>的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)/
}
]
}
}
我-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>
所以问题可能不在你提供的代码中。