thellmei
thellmei

Reputation: 243

NodeJS Sub-Modules

I've a node application and want to extract some code in sub modules (simple node modules with own dependencies). For now, I don't want separate git repositories or npm registry entries for that, but just provide them in subdirectories.

One of these modules is named core, because it should be used in the main node application as well as in other sub modules.

Let's say this is my structure:

  • myProject
    • package.json
    • app.js
    • core
      • index.js
      • package.json
    • partner
      • index.js
      • package.json

partner needs core and app.js needs core as well. My approach was to do the following:

cd partner
npm install --save ../core
cd ..
npm install --save ./core
npm install --save ./partner
npm install

which errored in:

npm ERR! code ENOENT

npm ERR! errno -2

npm ERR! syscall rename

npm ERR! enoent ENOENT: no such file or directory, rename '/home/tjorben/dev/myProject/node_modules/.staging/sliced-fc0a1515' -> '/home/tjorben/dev/myProject/core/node_modules/mquery/node_modules/sliced'

My goal ist, that I just have to make an npm install on the base folder (myProject) and it installs the dependencies for core and partner as well. Is there any way to achieve this?

Upvotes: 2

Views: 3403

Answers (1)

Spitzbueb
Spitzbueb

Reputation: 5871

You should have only one package.json file for your project meaning only the one in the myProject folder. You install all your dependencies for myProject, core and partner into that package.json.

You can now require all external dependencies with: require("depencency");

It doesn't matter if they are for core or partner or any other module. You can require them the same way.

If you want to require your own modules just use: require("./core");

Remember to use the correct path to the submodule (like ../core in your partner submodule).

Upvotes: 4

Related Questions