1

I was following the section "How do I use a web component?" from

What are web components?

end very soon I realized that node and npm it's a must. On top of that you have to run node to serve it.

Code

<html>
<head>    
    <script type="module" src="node_modules/@polymer/paper-button/paper-button.js"></script>
</head>
<body>    
    <paper-button raised class="indigo">raised</paper-button>
</body>
</html>

What is the simplest - no node - way to run this simple web component?

1
  • you don't need node nor npm to use HTMLElement, the question should read How to run package @polymer/paper-button without node? Commented Feb 2, 2021 at 14:51

2 Answers 2

4

Disregard everything "Polymer",
better yet... burn it down, throw your coffee over every monitor you see that title.

It was based on the V0 implementation of Web Components;
which basically was Google's lets-throw-something-at-the-wall-and-see-if-other-vendors-adopt-it attempt at forcing Web Components on us.

That whole V0 era is what gave "Web Components" a bad reputation, and catchy blog titles like
"The Broken Promise of Web Components"
(but everyone involved learned what not to do)

Since years, all Browsers vendors work together on V1

Web Components run in all modern Browsers.. period.. so all you need is a Browser..
no built steps.. no bundlers.. no parcel.. no Svelte-compiler... no Rome... no fuss

Use any online editor like JSFiddle or CodePen, or your own IDE, or use your F12 Sources panel
and with a few lines of code you are started

<my-component name="Alex"></my-component>
<script>
  customElements.define("my-component", class extends HTMLElement {
    connectedCallback() {
      this.innerHTML = `<h2>${this.getAttribute("name")} his very first Custom Element!</h2>`;
    }
  });
</script>

The 3 distinct (each can be used without the other) technologies making up "Web Components" help you built more complex Components

Soon you will read about Lit and Hybrids and Stencil and uCe, and all types of tools that make developing Web Components "easier"...
They are just that... tools; You pick one or two once you fully understand the technology

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

Comments

2

The simplest way would probably be to just include the script via a CDN like UNPKG https://unpkg.com/.

<html>
<head>    
    <script
        type="module"
        src="https://unpkg.com/@polymer/[email protected]/paper-button.js?module"></script>
</head>
<body>    
    <paper-button raised class="indigo">raised</paper-button>
</body>
</html>

1 Comment

It would be nice to show the steps to achieve that without relying on CDNs

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.