How to execute a script from custom NPM package?

Viewed 228

I have created a custom NPM package which (at the moment) does contain a script which I want to execute from within another project.

This is how my package looks like:

myScript.js

console.log("The script has been executed!");

package.json

{
  "name": "@myscope/my-script",
  "version": "0.0.1",
  "description": "Lorem ipsum",
  "main": "./src/myScript.js",
  "files": ["src/*"],
  "author": "Me",
  "license": "MIT",
  "type": "module",
  "scripts": {
    "test": "node src/myScriptjs"
  }

The folder structure looks like this:

src/
 - myScript.js
.gitignore
package.json

I have successfully added the package to my other project as a dev-dependency and now I would like to do something like:

yarn myscript

where the corresponding script in the project's package.json would be something like:

"scripts": {
  "myscript": "node point/to/myscript.js"
}

Just to clarify: I would like to avoid to write:

"myscript": "node node_modules/@myscope/my-script/src/myscript.js"

Instead I would like to have something like:

"myscript": "node @myscope/my-script myscript.js"

I believe that something like the above is possible if the script is added to the node_modules/.bin folder. How would I get my script into there? Or is there any other way to make my script executable other than referring to the path in node_modules?

Thanks for your help!

1 Answers
Related