简体   繁体   中英

Can you publish an npm package for a frontend JavaScript file?

A client would like to use an npm package to access our JavaScript file's functions instead of adding a script tag to their html. Is this possible? The only resource I've come across for creating an npm package is for NodeJS files ( https://docs.npmjs.com/creating-node-js-modules ).

This is possible but you cannot import the npm package and have it work in the browser without some sort of bundling system like webpack .

Alternatively, which is a bad idea, would be to ship your dist folder with node_modules in it. This isn't recommended for a multitude of reasons, but it will work.

Following approach might be suitable for your client's requirement.

  1. Create a NPM project using the javascript file you have
  2. Publish it on client's Github account as a package.
  3. Create a.npmrc file in root of your client's project.
  4. Add your package in.npmrc file and in packages.json both.

Refer this doc for using a package with.npmrc file: https://docs.github.com/en/packages/guides/configuring-npm-for-use-with-github-packages#installing-a-package ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM