简体   繁体   English

反应助手导入在生产中失败,但在开发中工作

[英]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.

相关问题 条件在本机生产中反应失败,但在开发中有效 - Condition fails in react native production, but works in development React NPM 包(Dragula)可用于开发但不能用于生产 - React NPM package(Dragula) works in development but not in production React 组件未在生产构建中呈现,但在开发构建中按预期工作 - React component not rendering in production build but works as expected in development build JavaScript在开发中发挥作用,而不在生产中发挥作用(Heroku) - JavaScript Works in Development, Not in Production (Heroku) React-开发和生产中的环境变量 - React - Env variables in development AND production React 应用程序无法通过其请求在生产中访问我的快速应用程序,但在开发中运行良好 - React app can't reach my express app in production with its requests but works fine in development React Framer motion animation 在开发中有效,但在生产构建中无效 - React Framer motion animation works in development but doesn't work in production build React.js 应用程序在开发模式下运行良好,但在构建(生产模式)后出现错误 - React.js app works fine in development mode but has errors after building (production mode) 反应导入根路径助手 - React import root path helper 如何在反应中导入Helper类 - how to import Helper class in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM