1

I want to launch an npm run script located in repository A from repository B (both cloned locally).

In repository A the script is defined like this in package.json:

"scripts": {
  "my-script": "node my-script.js"
}

... and it's launched with npm run my-script. It works fine when launched this way with no errors. The script compiles some files and copies them to different folders inside repo A.

So, my first thought to be able to run that process from repository B was to simply call node my-script.js from npm.

"scripts": {
  "my-script-in-other-repo": "node ../path/to/repo/my-script.js"
}

... and call it with npm run my-script-in-other-repo in repository B.

Unfortunately this approach throws a bunch of Node and NPM errors which do nothing to help solve this. Stuff like Make sure you have the latest version of node.js and npm installed.. Here is the output.

My intuition tells me it may be a problem with the Node context, or maybe Node in repository B can't access the dev packages in repository A.

A quick and dirty solution could be to replicate my-script.js in repository B changing the paths of the files and copying the same dependencies... but it is kinda ugly.

Is there a cleaner way to solve this?

Pier
  • 10,298
  • 17
  • 67
  • 113
  • 1
    1. You might've looked at the useless part of the error message. Can you show us the output verbatim? 2. Are you in control of **B**? If you are, you can make "my-script.js" a "binary" and run it as a dev dependency. 3. slight nitpick: npm is not an acronym, just keep it in lower case. ;) – E_net4 May 18 '16 at 20:54
  • Hey @E_net4 I've edited my question. Yes I'm in control of both repos. – Pier May 18 '16 at 21:04
  • The actual error lies before npm's error message, and is caused by `npm run bundle-css && npm run build-js && npm run build-js-dependencies && npm run sync-repos && npm run bundle-css-XXXXXXXXXXXXX`. You may wish to look into that. – E_net4 May 18 '16 at 21:07
  • You are right. I've scrolled up and Node complaints about a null variable when running from repository B. But when running from repository A the script works fine... – Pier May 18 '16 at 21:10
  • A null variable, you say? Or is it a missing module? Can you show a snippet? – E_net4 May 18 '16 at 21:13
  • `/Volumes/Data/Projects/Github/XXXXXX/bundle-css.js:18 fs.writeFile(sassResult, result.css, function () { ^ TypeError: Cannot read property 'css' of null at Object.callback (/Volumes/Data/Projects/Github/XXXXX/bundle-css.js:18:33) at options.error (/Volumes/Data/Projects/Github/XXXX/node_modules/node-sass/lib/index.js:274:32)` – Pier May 18 '16 at 21:31

1 Answers1

4

You seem to have your project A as a dependency of B, and that you have scripts in A that are intended to be executed directly as independent applications.Luckily, there is a painless way in npm to register such scripts and run them from dependents.

In project A, use the "bin" property in package.json to keep your executable scripts:

"bin": {
  "A-script": "my-script.js"
},

In project B, keep A as a dependency (or devDependency, depending on when you need it), then run A-script as if it were a command:

"scripts": {
  "my-script-in-other-repo": "A-script"
}

However note that, although module resolution still works when running scripts from a foreign working directory, resolving other kinds of resources using fs depends on the current working directory. According to the docs regarding fs operations:

The relative path to a filename can be used. Remember, however, that this path will be relative to process.cwd().

If you need to load a resource file (such as a css file) residing in some place relative to your source code, consider using the __dirname global variable in order to resolve to that file. This could have actually been the root of your problem.

E_net4
  • 27,810
  • 13
  • 101
  • 139