首页 > 解决方案 > Add dist file from npm package to build

问题描述

I'm using the node-poweredup npm package in my react based project. I provides a nodejs and a browser version of the library. The browser version is available from the jsdelivr CDN and this is also how I add this library to my react app - I simply include the poweredup.js in a script tag in the index.html.

I can see that the node-poweredup module has a the same file in its dist/browser folder (node_modules/node-poweredup/dist/browser/poweredup.js). How can I copy this js file to my build directory when running yarn build so that I can use this one instead of the one from the CDN?

I have a standard react app created with create-react-app.

标签: javascriptreactjsyarnpkg

解决方案


尝试正常导入。删除 CDN。react-scripts 会在构建过程中打包文件。

如果问题仍然存在,请将整个目录 node_modules/node-poweredup 复制到 node_modules 之外并执行绝对导入。

import * from './node-poweredup/${path}'

提供模块所在的路径

或者,更好的是下载 CDN 并使用 script 标签导入 index.js


推荐阅读