Giulio Serra
Giulio Serra

Reputation: 373

Create a npm package with dependencies

I have a distributed node.js project and I want to create a standalone package containing the project's domain.

First of all I created a package called "common" containing some utilities using the:

npm pack

command and creating the common.tgz file. Next I created the model.tgz file using the same method, please node that the model package use the common package so I installed it putting inside "model" in the temp folder, so the model package has the following structure:

|_ model
  |_ tmp
    |_common.tgz

It seems to work.

Now I want to import model.tgz inside my main project putting it into temp folder, but when I run the

npm i 

comand I get:

npm ERR! enoent ENOENT: no such file or directory, open 'prj_name/backend/functions/functions/tmp/common-1.0.0.tgz' npm ERR! enoent This is related to npm not being able to find a file.

I solved it by doing so in my main project:

|_ main
  |_ tmp
    |_model.tgz 
    |_tmp
      |_common.tgz

Since this is a little bit confusing, Is there a way to include common.tgz inside model.tgz and not need to re-import everywhere? Thanks.

Upvotes: 1

Views: 681

Answers (1)

Giulio Serra
Giulio Serra

Reputation: 373

Turns out there is a field you can use in the package.json:

 "bundledDependencies":[],

so in my case the package model has the following package.json:

 "dependencies": {
    "common": "file:tmp/common-2.0.0.tgz"
  },
  "bundledDependencies":["common"],

and the main project now just have:

- main
  |_ tmp
    |_model.tgz 
    

Upvotes: 0

Related Questions