首页 > 解决方案 > 无法在本地使用使用 webpack 创建的包

问题描述

我已经使用 webpack 创建了 demo_package,但无法在另一个包中使用以下是 demo_package 的 src/index.js、package.json 和 webpack.config.js 文件

src/index.js

 function hello(){
  console.log("hellow world")
 }
export default hello;

演示/包/package.json

{
 "name": "demo_pack",
 "version": "1.0.0",
 "description": "",
 "main": "dist/main.js",
 "scripts": {
   "test": "echo \"Error: no test specified\" && exit 1",
   "build": "webpack"
  },
 "author": "",
 "license": "ISC",
 "dependencies": {
   "webpack": "^4.41.2"
 },
 "devDependencies": {
    "webpack-cli": "^3.3.10"
 }
}

演示/webpack.config.js

const path = require('path');
 module.exports = {
 entry: './src/index.js',
 output: {
   filename: 'main.js',
   path: path.resolve(__dirname, 'dist'),
   },
 };

以下是我在测试中使用它的方式

var hello =require('demo_pack');
 hello();

错误是你好不是一个函数,我已经在本地链接了我的包,在此先感谢

标签: javascriptnode.jsnpmwebpack

解决方案


更新您的webpack.config.js.

const path = require('path');
 module.exports = {
 entry: './src/index.js',
 output: {
   filename: 'main.js',
   path: path.resolve(__dirname, 'dist'),
   publicPath: "",
   library:"Hello",
   libraryTarget:"umd",
   globalObject: "this"
   },
 };

推荐阅读