简体   繁体   中英

How do you find out which functions are exported by a node/npm package?

Following my earlier question , and the Mozilla documentation on import , I now understand that I must do something like the following to use the functionality in a module:

  1. import * as name from "module"; or
  2. import {functionName} from "module";

Coming from using CommonJS, I never thought about which functions were exported by a package because I just used to require them like:

const vueServerRenderer = require('vue-server-renderer') // get the module
vueServerRenderer.createRenderer() // use a function in that module

How can someone find out which functions are being exported by a module such as express or vueServerRenderer so I know how to use the correct import statement like:

import express from 'express' instead of import * as express from 'express' ?

You need to read the module source.

Every export statement exports something. It may be a function, an array, a string, a class etc.

Every export statement without default needs to be destructured on import:

import { NonDefaultThing1, NonDefaultThing2 } from 'somewhere'

An export statement with default must be imported directly without the {} :

import DefaultThing from 'somewhere'

Some modules have default export but also non-default exports. You can pick and choose what to import:

import DefaultThing, { NonDefaultThing7 } from 'somewhere'

If you use an IDE that can parse javascript such as Microsoft Visual Studio Code you can get autocompletion/intellisense of the import statement. There are even plugins that does auto-import: just use a class or function or something from a module and it will automatically add the required import statement at the top of your file.

TLDR: The default export.

Say a particular library named "module" has the following code

function functionName() {
  // function body
}

export default functionName;

Now, in your code, if you put

import blah from "module";

then blah will point to functionName .

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