Glob wildcards in windows npm

2019-03-20 12:45发布

问题:

I'm trying to get npm to do a build browserify on a folder of scripts. The problem is, I'm on windows and doing folder/*.js doesn't seem to work. I've tried globally installing glob, but whenever I run a build command, the error comes back saying "Cannot find module 'c:\www\project\static\js\components*.js'.

Here's my package.json:

{
  "name": "TEST",
  "description": "ITS ME MARIO",
  "author": "JJ",
  "version": "0.0.1",
  "dependencies": {
    "connect": "1.8.5",
    "express": "2.5.2",
    "jade": "0.20.0",
    "mongoose": "3.8.x",
    "socket.io": "0.8.7"
  },
  "devDependencies": {
    "vows": "0.5.x",
    "mocha": "*",
    "should": "*",
    "jshint": "latest",
    "browserify": "latest",
    "rimraf": "latest",
    "hashmark": "latest",
    "stylus": "latest",
    "glob": "latest"

  },
  "scripts": {
      "clean": "rimraf dist",
      "test": "mocha test/",
      "build:components-js": "browserify static/js/components/*.js > static/dist/components.js",
      "build:app-js": "browserify static/js/script > static/dist/app.js",
      "build:css": "stylus static/css/style.styl > static/dist/main.css",
      "build": "npm run build:css && npm run build:components-js && npm run build:app-js"

  },
  "engine": "node >= 0.6.6"
}

Anyone know what I'm doing wrong?

回答1:

I don't think you're doing anything wrong; this is basically a limitation of the Windows shell/console/command prompt, although browserify could be 'improved' to sidestep that and use glob / node-glob instead. I'm not sure about browserify, but jshint is similar.

Some ideas:

  • Try passing the root directory name instead. That's less powerful but seems to work well enough with jshint: https://github.com/jshint/jshint/issues/1904

  • Use cygwin as the shell you run npm from. It brings much *nix power to Windows.

  • Tweak or request a tweak to browserify (and jshint, and... ?) so that they invoke the glob library to handle these file-related parameters. Compare: https://github.com/jshint/jshint/issues/1998

  • Wrap said tools with 'translators' such https://www.npmjs.com/package/build-jshint . Note that this is explicitly designed to support ** wildcards etc.

Just guessing, but there might also be a way to

  • use PowerShell (which comes with recent versions of Windows--see the Get-ChildItem command)

  • or Hamilton C shell (uses ... instead of **, I think), or something else, as your shell.

  • use a loop with /r for recursing into subfolders. I'm not recommending this--Windows-specific, not very chainable--but the 'wint' command set up below does 'work' (invoke with npm run wint) if I include the following in my package.json file.

loop for Windows (Note: redirecting the output below to a file isn't a simple > because ...do jshint %f > chk.txt will overwrite itself and ...do jshint %f > %f.chk.txt may generate many chk.txt files sprinkled around):

"scripts": {
  "lint": "jshint **.js",
  "wint": "for /r %f in (*.js) do jshint %f",
},

But the commands above would generally not be usable cross-platform. Also, using an alternative shell, you don't benefit by default from being able to shift+right-click on a folder and "Open command window here".

Related:

  • Cannot use GLOB with JSHint in Windows?

  • https://superuser.com/questions/358863/wildcard-for-all-subdirectories-or-all-descendent-directories-in-windows-command



标签: node.js npm glob