简体   繁体   中英

Move script used in package.json to npm package

I have a file tools.js that sits in the root directory of each of our plugins. It has a build function which helps create a build for the plugin. It is used by the package.json file like so:

"scripts": {
    "build:node": "node -e 'require(\"./tools\").build()'",
}

Rather than needing to have this file included in every plugin's folder, I would like to put it into our node package which is required by all of our plugins, so that it can just be added by someone running npm install. That part is simple enough, but I don't know how to then access it and call one of its functions from the original package.json file.

This is probably straightforward but I don't have much experience with NPM so would really appreciate any advice.


Just to clarify, the folders currently look like this:

plugin1/
    package.json
    tools.js
    node_modules/
        our_node_package/     (where I want tools.js to be called from)

You should publish the package to NPM, and then you can use it by installing it.

Here is a guide to publishing your package to the NPM. https://zellwk.com/blog/publish-to-npm/

New Answer:

There are 2 possible ways:

  • Find a way to extend the functionality of the package that you want to call your package's function.
  • Fork the package, update the code, and re-publish it under your name. And use your forked package into the project

One way would be: your_npm_package is the published package name.

"scripts": {
    "build:node": "node ./node_modules/your_npm_package/tools.js",
}

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