3

I have an old project that uses this project it is basically just a JS file. I have tried adding it a few ways...

"jquery-highlight-regex": "jbr/jQuery.highlightRegex"

npm ERR! eisdir EISDIR: illegal operation on a directory, read
npm ERR! eisdir This is most likely not a problem with npm itself
npm ERR! eisdir and is related to npm not being able to find a package.json in
npm ERR! eisdir a package you are trying to install.

"jquery-highlight-regex": "https://raw.githubusercontent.com/jbr/jQuery.highlightRegex/master/highlightRegex.js"
npm ERR! not a package /var/folders/vf/mnj9dq116gvcww95cbk3r3n40000gp/T/npm-32798-d819bc7b/raw.githubusercontent.com/jbr/jQuery.highlightRegex/master/highlightRegex.js
npm ERR! Darwin 16.0.0
npm ERR! argv "/Users/.../bin/node" "/Users/.../apps/node/bin/npm" "install"

So how do I include a simple JS file as a dependency without a package.json?

Jackie
  • 21,969
  • 32
  • 147
  • 289
  • More generally, when there's is no package available on NPM but I want to be able to download a single-file JS dependency as part of `npm install` rather than commit it to my repository, how can I do that? – mjaggard Apr 12 '21 at 19:52
  • Would a hook on postInstall or similar be about to download the additional file somehow? – mjaggard Apr 13 '21 at 22:20

2 Answers2

4

You could use a postinstall script along with a dependency such as download-cli to pull the file manually. This may be useful if the file changes often, as in mjaggard's case.

The following example package.json will, upon install, fetch the file and place it in a /third-party folder.

{
  "name": "so-npm-download-on-install",
  "version": "1.0.0",
  "scripts": {
    "postinstall": "download --out third-party https://raw.githubusercontent.com/jbr/jQuery.highlightRegex/master/highlightRegex.js"
  },
  "dependencies": {
    "download-cli": "^1.1.1"
  }
}

Clone this example repo to try it out.

Zack Ream
  • 2,956
  • 7
  • 18
1

As you've already discovered, you can't reference remote files by URL in your package.json, but you do have a few options here:

  1. Commit the file to npm, either as a public or private package, then include it as a normal dependency.
  2. Fetch the file dynamically then run it as suggested in this thread (this seems like a really poor option for a file that doesn't change).
  3. Just copy the file and include it locally (not sure why you wouldn't do this since it doesn't seem like it's changed in 4+ years).
Tim
  • 2,843
  • 13
  • 25
  • Option 1 might be a possibility for me, I'll investigate. Option 2 is a bad idea for quite a number of reasons. Option 3 might work for the original question author but not for me - my library gets updated quite regularly. – mjaggard Apr 13 '21 at 22:19