Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import NPM package in JSFiddle?

I would like to use valid-url to validate some URLs using JSFiddle so that I can share it later. I tried adding a link to the index.js file from Github (https://raw.githubusercontent.com/ogt/valid-url/master/index.js) but Fiddle says:

Github is not a CDN, using it as such will cause issues with loading the file. Do you still with to it?

And obviously when I try to use it, an error is thrown:

Refused to execute script from [...] because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.

So, is there any way to use npm packages in a JSFiddle? Or any workaround to achieve this?

like image 412
josemigallas Avatar asked Oct 20 '17 08:10

josemigallas


People also ask

Is there any way to use npm packages in JSFiddle?

So, is there any way to use npm packages in a JSFiddle? Or any workaround to achieve this? You can't use code directly from GitHub, but you can include them from rawgit.com. Also, using RequireJS is probably a good idea because you won't be able to use the normal require (...) or import ... syntax on the web. Use unpkg.com.

How to use NPM modules in Node JS?

The default way of using npm modules in Node.js is called CommonJS. To import a module you can use the function require (id). It accepts id as a path to a module. To require a newly installed npm module, you just pass its name as id. Node.js will lookup for the module chalk in the node_modules folder inside the root of your project.

How do I import npm packages in replit?

How do I import npm packages in repl.it javascript 1 Go to side bar on the left side. 2 Click on the second option, which is packages. 3 Type the name of the module you are in need of. 4 Click on the name when it appears in the list below. 5 Click on the plus sign.

How do I import a node module?

To require a newly installed npm module, you just pass its name as id. Node.js will lookup for the module chalk in the node_modules folder inside the root of your project. After the import, the constant chalk will hold all the functionality exported by the module chalk. Another approach to importing node modules is called ES6 import.


2 Answers

Use unpkg.com. They allow you to load any npm module from the browser like a CDN.

like image 146
Lance Avatar answered Oct 19 '22 09:10

Lance


Using UNPKG you can load any file from any package following this pattern

https://unpkg.com/:package@:version/:file

So, if you add

https://unpkg.com/[email protected]/

Then you'll get this (as shown in the next image)

UNPKG npm package in JSFiddle

If you specify the index.js file

https://unpkg.com/[email protected]/index.js

Then you'll get this (as shown in the next image)

JSFiddle with npm packages

You'll then be able to use it in your fiddles by adding the Resources you want to.

like image 45
Tiago Martins Peres Avatar answered Oct 19 '22 08:10

Tiago Martins Peres