简体   繁体   中英

use node module in javascript

I want to import and use the node module in js.

The module i want to use is this.

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 install contentful-resolve-response --save
  1. Create an index.js file and import that module.
import resolveResponse from 'contentful-resolve-response'
  1. Use module like github example
var items = resolveResponse(response)
  1. Bundled with webpack.

  2. I put the bundled js file in html and tried to run it.

The error phrase is: Uncaught TypeError: n(...).resolveResponse is not a function

Is the module not properly imported?


package.json dependency

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

index.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.

Remove the asterisk from the import statement and then call the function with the name that you used for the import.

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.

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 * 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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