How can I run an npm script installed in the root of a learn repo, from a child package?

Viewed 265

For instance, I want to run jest tests for one of my packages.

I set up the test script in the child package.json:

"test" : "jest"

However when I got to the package directory and run:

npm test

I get:

sh: jest: command not found

This makes sense because I've only installed jest in the root package since it is a dev dependency.

What do I need to do to make the npm package jest available in the child packages?

1 Answers

We're using an npm package called env-cmd https://www.npmjs.com/package/env-cmd to run scripts from root level in packages.

our root package.json looks something like this:

  {
    "name": "@myAwesomeApp/root",
    "private": true,
    "devDependencies": {
      "env-cmd": "^10.1.0",
      "lerna": "^5.0.0"
    },
    "dependencies": {
      [...]
    },
    "workspaces": [
      "packages/*"
    ],
    "scripts": {
      "internal:warning": "echo \"\n\t\\\\033[32m! ANY NOTIFICATION !\n\"",
      "jest": "npm run internal:warning && env-cmd --silent  lerna run test"
    }
  }
Related