11

I have a script called myscript.js. I execute the script usually using this command:

$ node myscript.js

How can I include the JStat Library via the CDN address in the script:

//cdn.jsdelivr.net/jstat/1.2.1/jstat.min.js

1 Answer 1

14

There isn't any logic built into node's require to do this.

If you are certain that this library supports use on the server-side (not all libraries are "isomorphic", that is, usable on both client and server side), then you can download it and require it locally (require('./jstat.min.js')).

The best way would be to find it on npm and install it, like any other node module.

Note: There isn't really an advantage to using a cdn on the server side. The main use case for a cdn is providing a cacheable copy to users' browsers, but users' browsers won't be running node code.

Sign up to request clarification or add additional context in comments.

1 Comment

A lot of code examples on the web assume that you're running in a browser and provide script tags that load something from a CDN. However, I often want to just use that library in a simple nodejs script. That's a common use case. If the same library can be found in npm, that's great, but finding it might take too much time. And, of course, you wouldn't expect to use a library that assumes the presence of a DOM or other browser artifact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.