[英]react helper import fails in production but works in development
I've got a file called "globalHelper.js" like this:我有一个名为“globalHelper.js”的文件,如下所示:
exports.myMethod = (data) => {
// method here
}
exports.myOtherMethod = () => { ... }
and so on...等等...
I import my Helper in other files like this:我将我的助手导入到其他文件中,如下所示:
import helper from "../Helper/globalHelper";
Now there is the problem: In the past, everything worked with that when running my build-script:现在出现了问题:过去,在运行我的构建脚本时,一切都可以正常工作:
"build": "GENERATE_SOURCEMAP=false react-scripts build"
but for some reason, when I run "npm run build", I get the error:但由于某种原因,当我运行“npm run build”时,出现错误:
Attempted import error: '../Helper/globalHelper' does not contain a default export (imported as 'helper')
However, when I simply start my development server (npm start), everything works just fine.但是,当我简单地启动我的开发服务器(npm start)时,一切正常。
I already tried to import like import { helper } from "../Helper/globalHelper";
我已经尝试像
import { helper } from "../Helper/globalHelper";
, but that did not work either. ,但这也不起作用。 Does someone know how to solve that?
有人知道如何解决吗?
Since you've not export default
you should import the function with {}
like:由于您没有
export default
,您应该使用{}
导入 function,例如:
import {helper} from "../Helper/globalHelper";
try exporting like this with ES6 syntax尝试使用 ES6 语法像这样导出
export const myOtherMethod = () => { ... }
then然后
import { myOtherMethod } from '...your file'
The method you are using exports.functionName
is CJS.您使用的方法
exports.functionName
是 CJS。 you need to use require to get the methods.您需要使用 require 来获取方法。
The CommonJS (CJS) format is used in Node.js and uses require and module.exports to define dependencies and modules.
在 Node.js 中使用 CommonJS (CJS) 格式,并使用 require 和 module.exports 来定义依赖项和模块。 The npm ecosystem is built upon this format.
npm 生态系统建立在这种格式之上。
If you want to use module method you can do this.如果你想使用模块方法,你可以这样做。
export { myOtherMethod1, myOtherMethod2 }
then you import like this.然后你像这样导入。
import * as Module from '....file name'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.