How to make reference in package.json to another module of my project?

1

Hello my application created with node.js I have several modules: the database, the Api, customer service, etc ... Well, in some I have to refer to another in the package.json file but I do not know if I'm doing it right, because when I start linking them, it does not end up working. I have all the modules in the same directory, each one in your directory. The package.json file sometimes flags me the error: this module is not installed , because if it is in node_modules . Am I calling the module well in the package.json file? "michaelgram-client": "file:../michaelgram-client" , Thanks, I show part of package.json

  

EDIT: To add code that was suggested to me.

"license": "MIT",
  "dependencies": {
    "aws-sdk": "^2.239.1",
    "axios": "^0.18.0",
    "babel-polyfill": "^6.26.0",
    "body-parser": "^1.18.3",
    "concurrently": "^3.5.1",
    "cookie-parser": "^1.4.3",
    "empty-element": "^1.0.0",
    "express": "^4.16.3",
    "express-session": "^1.15.6",
    "file-extension": "^4.0.2",
    "intl": "^1.2.5",
    "intl-messageformat": "^2.2.0",
    "intl-relativeformat": "^2.1.0",
    "materialize-css": "^0.100.2",
    "michaelgram-client": "file:../michaelgram-client",
    "multer": "^1.3.0",
    "multer-s3": "^2.7.0",
    "node-pre-gyp": "^0.9.1",
    "page": "^1.8.6",
    "passport": "^0.4.0",
    "passport-facebook": "^2.1.1",
    "passport-local": "^1.0.0",
    "pug": "^2.0.3",
    "readable-stream": "^2.3.6",
    "superagent": "^3.8.3",
    "title": "^3.2.0",
    "vinyl-source-stream": "^2.0.0",
    "yo-yo": "^1.4.1"
  },
  

THIS IS THE MODULE I BRING TO THE MODULE THAT GIVES ME THE PROBLEMS

//PACKAGE.JSON DEL MODULO QUE TRAIGO

{
  "name": "michaelgram-client",
  "version": "1.0.0",
  "description": "Michaelgram API Client",
  "main": "index.js",
  "scripts": {
    "lint": "standard",
    "test": "npm run lint && ava"
  },
  "standard": {
    "parser": "babel-eslint"
  },
  "author": "Miguel Espeso ",
  "license": "MIT",
  "devDependencies": {
    "ava": "^0.25.0",
    "babel-eslint": "^8.2.3",
    "nock": "^9.2.6",
    "standard": "^11.0.1",
    "uuid-base62": "^0.1.0"
  },
  "dependencies": {
    "bluebird": "^3.5.1",
    "request": "^2.34.0",
    "request-promise": "^4.2.2"
  }
}
    
asked by Miguel Espeso 23.05.2018 в 15:44
source

1 answer

1

Simplifying the structure of the project.

Project structure

~root
  - moduloA
     - index.js
     - ...
  - moduloB
    -index.js
    - ...

Suppose I am doing the moduloA module whose input file ( main ) is index.js . In that package.json there must be:

{
 ...
 "main": "index.js",
 ...
}

Now we are working on another module moduloB .

If we want to install the local module moduloA taking into account the structure of the project, we would do:

npm install ../moduloA --save

Anywhere in module B you can load your module:

import ... from 'moduloA' (o require('moduloA'))
    
answered by 24.05.2018 / 15:33
source