'Custom NPM package - Custom command line command

I recently created an NPM package called Project_1 (which I installed in another of my Node.js projects called Project_2) using the command:

 npm install --save ./path/to/Project_1

(Is there a better method to install a local package inside one other locally?)

So the packaje.json of Project_2 is as follows:

{
  "name": "Project_2",
  "main": "index.js",
  "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  },
  "dependencies": {
    "Project_1": "file:../Project_1",
  }
}

In the npm package I created (Project_1), the JSON package is as follows:

{
  "name": "Project_1",
  "scripts": {
    "custom-serve": "http-server ./website --port 8888 -o"
  }
}

When I am in the root of Project_1 I can launch from the terminal:

npm run custom-serve

And in this way I can execute my custom command.

I need to know how to call npm run custom-serve inside Project_2 (also from the command line) after installing the Project_1 package as an npm dependency in Project_2. So, in the root of Project_2 I would like to be able to run npm run custom-serve so that the command written in the Project_1 library is triggered.

How can I do this? How should I set the JSON packages? Is there a best practice for doing this? Do I need to add special scripts in .js? This is because I have noticed that always when installing npm packages they provide commands that can be launched from the root of the project in which they are installed.

NB: The command custom-serve is just an example. I would like to create some custom commands inside the Project_1 and i want to be able to call them inside the Project_2 after the npm install of the Project_1 package. I have already tried to create a script inside the Project_2 like so:

"scripts": {
      "custom-command": "cd ./node_modules/Project_1 && npm run custom-serve",
}

But it doesn't works.



Solution 1:[1]

Firstly, to answer your question if there's a better way to install a local dependency, there is via npm link.

Assuming the name in Project 1's package file is project-1, you can link it in Project 2 as follows (obviously using the paths that correspond to your setup):

cd ~/projects/project-1
npm link
cd ~/projects/project-2
npm link project-1

Secondly, if you want your package to expose a runnable command, you should configure this in the bin section of its package.json. The path should point to an executable file, so to reuse the npm script, you could use execute as follows

custom-command.js

const {exec} = require('child_process');
exec('npm run custom-serve');

And then add the bin section to the package.json of Project 1:

"bin": {
  "custom-command": "./custom-command.js",
}

Solution 2:[2]

If you have an index file inside your Project_1, and you had Project1 as a dependency for Project2, you can just call the file and it will run the default start command:

In Project_1 package.json file:

{
  "name": "Project_1",
  "scripts": {
      "custom-serve": "http-server ./website --port 8888 -o",
      "start": "npm run custom-serve"
  }
}

In Project_2 package.json file:

{
  "name": "Project_2",
  "scripts": {
    "custom-command": "node Project_1"
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mohamad Kamar