James Conway
James Conway

Reputation: 349

Node packaged dependencies

I have a node.js project that uses a dependency which I have listed in the package.json. It all works fine with the dependency however I want to be able to ship my project without people having to download the dependencies. How can I copy the dependency into my project and still use it with require('') in my project files?

Upvotes: 2

Views: 251

Answers (1)

Dimitri Kopriwa
Dimitri Kopriwa

Reputation: 14353

This depend on your delivery flow. (In what environment, what kind of application, what tools, how and where will you deliver, ...)

If the dependency is present in your package.json dependencies, then you can retrieve it by doing npm install --production and use require('lib'); within your source code as usual.

It is also possible to set your environment NODE_ENV=production and just run npm install.

If you can't run npm, I would suggest you to include the node_modules in your distributed package.

Update

After chatting with your, it appear that the dependency is probably not installed with npm.

It is hooked in a system that can use git repository to install plugins.

Because you use a git repository for hosting your module, you should be able to keep node_modules into .gitignore when you install the module with npm install git+https://github.com/namespace/repo.git, it will installed automatically becauuse of npm install reading the package.json.

If your client does not rely on npm, then you'r only option is to keep node_modules on your github repo.

In this case your import should look like:

require('./node_modules/module-a');

Maybe there is a third option, check with your client software if it can install from npm registry, if this is the case, then you should be able to ignore node_modules from the repo, and if it can't then it means you can't use github repository for npm installation, instead use a real npm registry like https://registry.npmjs.com or host your own one with https://github.com/verdaccio/verdaccio

Upvotes: 2

Related Questions