简体   繁体   中英

What's the best development workflow for building an NPM package?

I am currently trying to package a javascript library. So far I have the package set up as follows:

  • npm build puts everything in a build directory
  • main attribute in package.json points to the entrypoint in build that exports my libary's top-level API.
  • I am able to include the package in another project by adding "{packagename}": "file:{pathToMyPackage}"

My question is this: I am now trying to troubleshoot my package from the other project. Each time I make changes, I must rebuild the project to reflect the changes AND I must rm -rf node_modules/{packagename} && npm install on the project that is using the local package.

I know I can add some kind of watcher to the package that will build when new files are saved, but how can I make the higher-level project monitor changes to the local package it is using? Is there a magic tool for this sort of thing, or do people just add custom npm scripts while they are doing development on a dependency?

Thanks!

What you're looking for is npm link ( https://docs.npmjs.com/cli/link ).

Example Usage:

package-a depends on package-b .

Navigate to package-b 's project folder on the command line. Run npm link .

Now navigate to package-a 's folder and run npm link package-b (You may need to run npm uninstall package-b first; not sure).

This will create a symlink in package-a/node_modules/package-b to package-b 's working directory. Any changes you make there will be reflected in the node_modules for package-a .

Just keep this in mind; if you break something while working on package-b , package-a may break too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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