npm install dependencies of local modules

2019-07-31 10:02发布

I'm having trouble using npm with a local module. My project's structure looks like:

package.json
local_module/
    - package.json
    - gulpfile.json
gulpfile.js

The main project's package.json is essentially:

{
    "dependencies": {
        "local_module": "file:local_module"
    },
    "devDependencies": {
        "gulp": "..."
    }
}

The local module's package.json is essentially:

{
    "scripts": {
        "prepublish": "gulp release"
    },
    "devDependencies": {
        "gulp": "..."
    }
}

My intention is keep my project modular by keeping local_module as its own package that is used as a dependency for the main project. I want to run npm install in the main project and use local_module from node_modules. However, local_module needs gulp installed to run the prepublish step, and when running npm install from the main project, it does not install the dependencies for local_module, and so gulp isn't installed, so it can't do the prepublish step.

Several questions like this have been asked, like NPM doesn't install module dependencies, but many are old and there are so many versions of npm that I can't get a clear solution.

How can I get npm to install local_module's dependencies before the prepublish step? I tried adding a preinstall step for the main project, e.g.

"preinstall": "cd local_module && npm install"

But it seems npm tries to run the prepublish step of local_module before running the preinstall for the main project. I want a solution that will do this in one npm install step rather than having a separate step before this to do npm install in the local module.

标签: npm
1条回答
萌系小妹纸
2楼-- · 2019-07-31 10:27

I have found a solution that will work for me in the immediate future. I changed local_module's package.json to:

{
    "scripts": {
        "prepublish": "npm install --ignore-scripts && gulp release"
    },
    "devDependencies": {
        "gulp": "..."
    }
}

When npm install is run from the main project, the prepublish step is run first in local_module, so I force prepublish to also do an install so that gulp is available to do the actual prepublish step. This is hardly ideal however.

查看更多
登录 后发表回答