Importing Node Modules With JavaScript

后端 未结 2 1436
予麋鹿
予麋鹿 2021-01-17 20:04

I apologize for the simple question, but I\'m pretty new to web development and JavaScript.

I want to import a package I\'ve installed using npm, specifically shopif

相关标签:
2条回答
  • 2021-01-17 20:37

    If you want to use npm modules via the syntax, like import sth from "something" for browsers, you'd need to set up a module bundler and ES6 compiler, such as Webpack and Babel. You'd need to google them and find tutorials for setting up them accordingly.

    An easy way to use the SDK seems to be using the CDN, since it's already been built for browsers to understand. Something like:

    index.html

    <script src="http://sdks.shopifycdn.com/js-buy-sdk/v1/latest/index.umd.min.js"></script>
    <script src="javascript/shopify.js"></script>
    

    shopify.js

    const client = ShopifyBuy.buildClient({
        domain: 'your-shop-name.myshopify.com',
        storefrontAccessToken: 'your-storefront-access-token'
    });
    
    console.log(client); 
    
    0 讨论(0)
  • 2021-01-17 20:41

    JavaScript Modules can only be run in module-mode scripts. Change your HTML to the following:

    <script type="module" src="javascript/shopify.js"></script>
    
    0 讨论(0)
提交回复
热议问题