简体   繁体   English

您可以为前端 JavaScript 文件发布 npm package 文件吗?

[英]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. 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 ).我遇到的用于创建 npm package 的唯一资源是用于 NodeJS 文件( https://docs.npmjs.com/creating-node-js )。

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 .这是可能的,但您不能导入 npm package 并让它在没有某种捆绑系统(如webpack )的浏览器中工作。

Alternatively, which is a bad idea, would be to ship your dist folder with node_modules in it.或者,这是一个坏主意,将您的 dist 文件夹与node_modules一起发送。 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使用您拥有的 javascript 文件创建 NPM 项目
  2. Publish it on client's Github account as a package.将其作为 package 发布到客户的 Github 帐户上。
  3. Create a.npmrc file in root of your client's project.在客户项目的根目录中创建一个 .npmrc 文件。
  4. Add your package in.npmrc file and in packages.json both.将 package 添加到 .npmrc 文件和 packages.json 中。

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 ). 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 ).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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