How will you import external libraries in Node.js?

CWC
2 Min Read

How will you import external libraries in Node.js?

When working in Node, you may want to include one or more libraries in your web part. Unfortunately, loading external Javascript libraries is often a conundrum.

Some developers prefer to use Typings as much as possible, which sometimes gets the job done, but some Javascript libraries just aren’t available this way at this point. This option, like many others, tends to deliver a lot of failed attempts at downloading external libraries and time wasted. Typescript, for example, won’t be able to read normal Javascript libraries for the simple reason that normal JS libraries don’t include definition files. This is exactly where Typings come in and they work as a great workaround, allowing the installation of definition files for the selected external libraries.

Here are some other options

Using “requires” to load your own files

In Node.js, you can use the node package manager (NPM) to install packages that your application can use. Express is a frequently used and efficient framework for loading web applications and you can get that through NPM.

Bundling a script

The web part bundler will by default include any library that is a dependent of the web part module. In this case, any relevant library will simply immediately be deployed within the same JavaScript bundle file as the web part. This is a really handy trick for smaller libraries, as long as they’re not being used by multiple web parts.

Loading a script from a CDN

Instead of loading the library from a NPM package, some prefer to use scripts via a CDN. It’s as simple as editing the config.json file and loading the library from its CDN URL. While this solution is also fairly straightforward, any one of these methods will take some tweaking and additional work, depending on the particular external library you’re looking to import.

[vc_row][vc_column][td_block_21 separator=”” tag_slug=”node-js” limit=”40″ tdc_css=””][/vc_column][/vc_row]

TAGGED:
Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version