Javascript 在ReactJS中导入模块后,npm start不会编译

Javascript 在ReactJS中导入模块后,npm start不会编译,javascript,node.js,reactjs,Javascript,Node.js,Reactjs,我正在使用CreateReact应用程序基于RedditAPI使用 我在“C:..\reddit gallery”中使用了npm安装--保存reddit.js 我编写了一些基本代码,当使用npm start运行它时,我不断得到: 未能编译 ./src/App.js中出现错误 C:..\reddit gallery\src\App.js 18:9未定义错误“reddit”无未定义 编码时,它会识别库,但使用npm时,它不会识别该库中的任何代码 守则: import 'reddit.js'; cl

我正在使用CreateReact应用程序基于RedditAPI使用

我在“C:..\reddit gallery”中使用了
npm安装--保存reddit.js

我编写了一些基本代码,当使用
npm start
运行它时,我不断得到:

未能编译

./src/App.js中出现错误

C:..\reddit gallery\src\App.js

18:9未定义错误“reddit”无未定义

编码时,它会识别库,但使用npm时,它不会识别该库中的任何代码

守则:

import 'reddit.js';

class App extends Component {

constructor(props){
    super(props);

    this.state = {};

    this.loadSubreddit = this.loadSubreddit.bind(this);
}

loadSubreddit(subre){
    reddit.hot(subre)
        .then(response => {
            return response.json();
        }).then(json => {
            console.log(json)
    })

}

componentWillMount() {
    console.log('comp is mounting');
    this.loadSubreddit('cats');
}

此库未准备为模块,因此您无法导入它。您只能通过HTML页面上的
标记将其包括在内。

它正在通过npm启动,但在npm测试和控制台中获得“TypeError:\u reddit2.default.hot不是函数”不理解您的意思。根据
CommonJS
您必须定义要加载到的引用的名称?通过语句
import'reddit.js'
您可以加载库,但您不能使用它,因为您没有对它的任何引用。另外,您必须了解,CLI模式下的node.js没有
窗口
对象,在这种情况下,您不能像在浏览器中那样从它调用
reddit
。您的意思是什么“定义您要加载的引用的名称”?请查看答案。大多数npm模块都是以这种方式导入的。在这种情况下,您应该定义变量
reddit
(例如),并将模块“导入到它”。然后您可以通过此变量调用模块。