使用webpack创建npm包

时间:2016-02-01 13:08:52

标签: node.js npm package webpack

我正在创建一个npm包并使用webpack作为babel,eslint等加载器。但是我假设包的最终编译版本应该只包含那个模块,没有{{3 }}

我目前的webpackBootstrap。我把它剥离下来,然后做好了工作"。

我检查它是否有效的步骤:

npm install
npm run build
npm install -g .
node
var test = require('test-package');

导致此错误:

Error: Cannot find module 'test-package'
    at Function.Module._resolveFilename (module.js:337:15)
    at Function.Module._load (module.js:287:25)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at repl:1:12
    at REPLServer.defaultEval (repl.js:248:27)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:412:12)
    at emitOne (events.js:82:20)

我是webpack和npm的新手,所以如果您需要更多信息请告诉我。

1 个答案:

答案 0 :(得分:12)

output.libraryTarget设为umd。这将为您提供从各种模块系统(全球,AMD,CommonJS)轻松使用的东西。

output.library是另一个有用的字段。这应该与您想要的库全局名称相匹配。

除此之外还有另一个问题。要使用导入工作npm link。此功能在开发过程中非常有用。您可以通过npm unlink还原链接。