简体   繁体   English

如何在本地开发npm模块

[英]How to develop npm module locally

Lets say im working on an app, MyApp, and I want to build an NPM module for it, MyModule. 让我说我正在开发一个应用程序,MyApp,我想为它构建一个NPM模块,MyModule。 Right now I can think of two ways to develop it: 现在我可以想到两种方法来开发它:

  1. Makes changes -> save -> npm install /path/to/module in MyApp 在MyApp中进行更改 - >保存 - > npm install / path / to / module
  2. Same as 1, except run npm install /path/to/module then editing it directly in node_modules then copying the changes over. 与1相同,除了运行npm install / path / to / module,然后直接在node_modules中编辑它,然后复制更改。

What I'd like is an easier workflow. 我想要的是一个更简单的工作流程。 One where I can simply save the file, refresh the page, and my changes are there. 一个我可以简单地保存文件,刷新页面,我的更改就在那里。 Is that possible? 那可能吗? For example, I know in Gemfiles I can just link to another directory as the path. 例如,我知道在Gemfiles中我只能链接到另一个目录作为路径。 Pretty sure I can't do that with npm tho. 很确定我不能用npm tho做到这一点。

You're looking for the npm link command , which is a two steps process: 您正在寻找npm link命令 ,这是一个两步过程:

  1. Run npm link from your MyModule directory: this will create a global package symlinked to the MyModule directory 运行npm linkMyModule目录:这将创建符号链接到一个全球性的包MyModule目录
  2. Run npm link MyModule from your MyApp directory: this will create a MyModule folder in node_modules , symlinked to the global symlink (and thus to the real location of MyModule ). MyApp目录运行npm link MyModule :这将在node_modules创建一个MyModule文件夹,符号链接到全局符号链接(从而到达MyModule的实际位置)。

要添加到Paul的答案,您还可以通过在MyApp目录中执行以下操作来执行上述操作的快捷方式:

npm link ../MyModule

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

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