简体   繁体   English

在 javascript 中使用节点模块

[英]use node module in javascript

I want to import and use the node module in js.我想在js中导入并使用node模块。

The module i want to use is this.我想使用的模块是这个。

https://github.com/contentful/contentful-resolve-response https://github.com/contentful/contentful-resolve-response

I ran it like the example, but encountered an error.我像示例一样运行它,但遇到错误。

  1. I installed the module with the npm command.我使用 npm 命令安装了模块。
npm install contentful-resolve-response --save
  1. Create an index.js file and import that module.创建一个 index.js 文件并导入该模块。
import resolveResponse from 'contentful-resolve-response'
  1. Use module like github example使用类似 github 示例的模块
var items = resolveResponse(response)
  1. Bundled with webpack.与 webpack 捆绑在一起。

  2. I put the bundled js file in html and tried to run it.我把打包好的js文件放在html中,尝试运行。

The error phrase is: Uncaught TypeError: n(...).resolveResponse is not a function错误短语是:Uncaught TypeError: n(...).resolveResponse is not a function

Is the module not properly imported?模块是否未正确导入?


package.json dependency package.json 依赖

"dependencies": {
    "contentful-resolve-response": "^1.3.12"
  },

index.js索引.js

import resolveResponse from 'contentful-resolve-response'

var response = {
  items: [
    {
      someValue: 'wow',
      someLink: { sys: { type: 'Link', linkType: 'Entry', id: 'suchId' } }
    }
  ],
  includes: {
    Entry: [
      { sys: { type: 'Entry', id: 'suchId' }, very: 'doge' }
    ]
  }
};

var items = resolveResponse(response) <- **error**

*Edited *编辑

The import statement was wrong, so I fixed it.导入语句是错误的,所以我修复了它。

import * as contentfulReserve from 'contentful-resolve-response'

to

import resolveResponse from 'contentful-resolve-response'

and call并打电话

var res = resolveResponse(response)

But this time I get the following error:但是这次我收到以下错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'type') error occurs. Uncaught TypeError: Cannot read properties of undefined (reading 'type')错误发生。

Remove the asterisk from the import statement and then call the function with the name that you used for the import.从导入语句中删除星号,然后使用您用于导入的名称调用 function。

import resolveResponse from 'contentful-resolve-response'

var response = {
  items: [
    {
      someValue: 'wow',
      someLink: { sys: { type: 'Link', linkType: 'Entry', id: 'suchId' } }
    }
  ],
  includes: {
    Entry: [
      { sys: { type: 'Entry', id: 'suchId' }, very: 'doge' }
    ]
  }
};

var items = resolveResponse(response)

The error Uncaught TypeError: n(...).resolveResponse is not a function alludes to the fact that the function resolveResponse is not a defined function in the scope that you are trying to use it.错误Uncaught TypeError: n(...).resolveResponse is not a function暗示 function resolveResponse不是您尝试使用的 scope 中定义的 function。

The thing that sticks out to me is the fact that your import statement imports all the exports of the library under an alias contentfulResorve which means if you would like to access the resolveResponse function from that library you must access it through the alias as contentfulResorve.resolveResponse .对我来说突出的是你的 import 语句以别名contentfulResorve导入库的所有导出,这意味着如果你想从该库访问resolveResponse function 你必须通过别名访问它作为contentfulResorve.resolveResponse .

import * as contentfulResorve from 'contentful-resolve-response'
/* SOME CODE HERE */
var items = contentfulResorve.resolveResponse(response)

However, looks like the default export on the library is the function you intend to use which means you can write it the following way但是,看起来库上的默认导出是您打算使用的 function,这意味着您可以按以下方式编写

import resolveResponse from 'contentful-resolve-response'
/* SOME CODE HERE */
var items = resolveResponse(response)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM