npm scripts nodemon - 观察 js 和 scss 文件的变化

npm scripts nodemon - watching js and scss files for changes

我正在尝试设置开发环境以仅使用 NPM,而不使用 grunt.js 或 bower.js。

我遵循了这个教程:http://beletsky.net/2015/04/npm-for-everything.html

我正在使用 nodemon 来监视我的 .js 和 .scss 文件以查找重新启动节点服务器的更改。所以在我的 package.json 文件中,在脚本下我有

脚本:

"watch-js": "nodemon -e js --watch public/js -x \"npm run build-js\"",

"watch-sass": "nodemon -e scss --watch public/sass -x \"npm run build-sass\"",

"watch": "npm run watch-js & npm run watch-sass"

但是当我 运行 npm run watch 它只监视 public/js 文件的变化。它会相应地触发构建。

但它不会监视 sass 个文件。

版本: node v0.10.36nodemon v1.4.1

我还包含一个构建脚本,如果我 运行 将 sass 编译为 css,那么我的构建-sass 脚本应该没问题

"build": "npm run build-js & npm run build-sass",
"watch": "npm run watch-js & npm run watch-sass"

如果您使用 windows,您可能会遇到 the windows problem

Because npm is reliant on the operating systems shell to run scripts commands, they can quickly become unportable. While Linux, Solaris, BSD and Mac OSX come preinstalled with Bash as the default shell, Windows does not. On Windows, npm will resort to using Windows command prompt for these things

如果是这样,您可以尝试使用 parallelshell or npm-run-all 以获得更好的跨平台支持。

这对我有用 Mac OS

  1. 安装nodemon

    npm install --save-dev nodemon

  2. 安装 npm-运行-all:

    npm install --save-dev npm-run-all

  3. 在package.json中添加两个脚本:

    "startn": "nodemon ./app.js", "watch-jscss": "gulp watch"

    • 其中 'app.js' 是您的服务器文件
    • "gulp watch" 可以替换为执行 sass watch
    • 的任何任务
  4. 在控制台中:

    npm-run-all --parallel watch-jscss startn