【问题标题】:react failed to compile module "conversational-form"反应未能编译模块“对话形式”
【发布时间】:2017-07-18 11:09:59
【问题描述】:

我正在尝试将这个 conversational-form npm module 导入到我的 Create-React-App 应用程序中,但在任何尝试这样做时都会遇到编译错误。

这是确切的错误:

./~/conversational-form/dist/conversational-form.js
Module not found: Can't resolve 'conversationalform' in '/Users/user/Desktop/app_name/node_modules/conversational-form/dist'

到目前为止,我已经尝试像这样导入它

import ConversationalForm from 'conversational-form';

或使用“dist”文件夹中的js文件的绝对路径,甚至只是将那个js文件包含在我的项目中,但总是会出现编译错误。

模块is supposed to work with React

任何想法可能导致这种情况?也许 Create-React-App 与此不兼容?我应该只包含该模块中的 JQuery 插件吗?谢谢。

更新:模块现在似乎已修复,但在尝试将示例调整为 Webpack/React/Component 结构时仍然出现错误

错误:

TypeError: Cannot read property 'setAttribute' of undefined

componentDidMount(){
13 |    // add Conversational Form info
> 14 |  this.refs.name.setAttribute('cf-questions', "Your name?");
15 |    this.refs.email.setAttribute('cf-questions', "Your email?");
16 |    this.refs.description.setAttribute('cf-questions', "What is description?");

代码

    import React, { Component } from 'react';
    import { CSSTransitionGroup } from 'react-transition-group';
    import ConversationalForm from './conversational_form';

    class ConversationForm extends Component {

        constructor(props) {
            super(props);
            this.cf = null; // <-- Conversational Form ref
        }

        componentDidMount(){
            // add Conversational Form info
            this.refs.name.setAttribute('cf-questions', "Your name?");
            this.refs.email.setAttribute('cf-questions', "Your email?");
            this.refs.description.setAttribute('cf-questions', "What is description?");
            this.cf = window.cf.ConversationalForm.startTheConversation({
                formEl: this.refs.form,
                context: document.getElementById("cf-context"),
                    flowStepCallback: function(dto, success, error){
                        success();
                    },
                    submitCallback: function(){
                    // this callback could also be added to the React.createElement it self...
                        alert("You made it!")
                        console.log("Form submitted...");
                    }
            });
        }
        render() {

            var conversation;

            if (this.props.visible) {
            conversation = 
            <div>
                    <form id="form" ref={form => this.input = form} className="form">
                        <input type="text" ref={name => this.input = name} placeholder="Name (required)" defaultValue={this.props.name} />
                        <input type="email" ref={email => this.input = email} placeholder="Email" defaultValue={this.props.email} />
                        <textarea ref={description => this.input = description} placeholder="Description" defaultValue={this.props.description} />
                        <button type="submit">Submit</button>
                    </form>
                    <section id="cf-context" role="cf-context" cf-context>

                    </section>
                </div>
            }

            return (
                <CSSTransitionGroup
              transitionName="success"
              transitionEnterTimeout={500}
              transitionLeaveTimeout={300}>
                {conversation}
            </CSSTransitionGroup>
            )
        }
    }

    export default ConversationForm;

【问题讨论】:

  • 你不想要一个 react-conversational-form 模块吗?

标签: reactjs npm create-react-app


【解决方案1】:

错误的原因可能是您使用导入的方式。由于ConversationForm 可能是模块中的几个组件之一 你需要像这样导入它:

import {ConversationForm} from 'react-conversational-form';

---编辑---

这是错误的,模块本身似乎有问题。

---更新---

模块背后的人非常好,并且已经发布了修复程序。问题似乎出在 webpack 处理依赖项的方式上。现在该模块按预期工作:

import cf from 'conversational-form'

var myForm = document.querySelector('#my-form');
var c = new cf.startTheConversation({formEl: myForm})

【讨论】:

  • 我刚刚看了npmjs.com/package/react-conversational-form,如果这就是你的意思,它看起来是空的,并且与我链接的模块无关?
  • 你是对的,它是空的:/ 无论如何,你遇到的问题似乎与导入有关。让我编辑答案。
  • 我尝试按照您的建议导入它,从 'conversational-form' 导入 { ConversationalForm } 并没有成功。该模块应该与 React 一起使用。见github.com/space10-community/conversational-form/blob/master/…
  • 我认为您毕竟是在正确导入它,并且该模块似乎发生了一些有趣的事情。如果您查看模块源文件(最底部),您可以看到行 define(["conversationalform"], function(conversationalform){module.exports = (root.conversationalform = factory(require("conversationalform"))); 如果您将(仅限字符串!)替换为破折号版本(“conversation-form”),解决错误就会消失。但是,我仍然很难让它工作。也许值得通过 CDN 导入?
  • 感谢您为此付出的努力。该模块肯定发生了一些奇怪的事情。我想考虑通过 CDN 导入它,但是如何将外部脚本导入 React 组件?
猜你喜欢
  • 2018-01-31
  • 2021-04-23
  • 2021-06-04
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 2020-01-23
  • 2016-08-11
  • 2022-09-28
相关资源
最近更新 更多