如何在npm scripts中使用nodemon构建和启动脚本?
How to use nodemon in npm scripts to build and start scripts?
"scripts": {
"build": "babel src -d lib",
"start": "node --use_strict ./lib/index.js",
"watch": "nodemon lib/index.js --exec npm run build"
}
使用命令 npm run watch
导致以下错误命令 运行:[nodemon] starting "npm lib/index.js run build"
我如何编写一个 nodemon 命令,在重新加载时使用 babel 转译代码并重新加载代码?
您可以简单地 运行 您的代码 babel-node
以避免显式转译。
$ nodemon lib/index.js --exec babel-node --presets=es2015,stage-2
似乎 this is the recommended 将 nodemon
与 babel
结合使用的方法。
请注意,运行宁 --exec
可能 在远程 运行宁你的 development
环境时产生意想不到的副作用你的 localhost
更好的选择是不使用全局安装,而是使用本地安装的包。这也将有助于根据 12 因素应用程序设计与本地机器设置相同的自动化构建。
"scripts": {
"watch": "node ./node_modules/nodemon/bin/nodemon.js"
}
您可以有两个 nodemon,一个用于转译,另一个用于 运行 您的代码。在 package.json 你可以这样做:
"scripts": {
"serve": "nodemon --watch dist/ ./dist/index.js",
"build" : "nodemon --watch src/ --exec babel ./src --out-dir ./dist --source-maps --copy-files"
},
"scripts": {
"build": "babel src -d lib",
"start": "nodemon --exec babel-node lib/index.js",
"serve": "npm run build && node lib/index.js"
}
Serve 用于生产环境,使用 npm start 你所做的首先是转译,然后 运行 nodemon。
有一个选项可以在 "watch" 模式下使用 Babel 构建文件,让 Nodemon 仅监视 "build" 文件夹并在编译输出发生更改时重新启动应用程序。
{
"name": "app",
"version": "1.0.0",
"private": true,
"dependencies": {},
"devDependencies": {
"@babel/cli": "^7.6.0",
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"nodemon": "^1.19.2"
},
"scripts": {
"build": "babel src --out-dir build --source-maps=inline --verbose",
"start": "yarn build --watch & sleep 1 && nodemon --watch build build/index.js"
}
}
此示例取自 GitHub 上的 GraphQL API Examples
存储库。
"scripts": {
"build": "babel src -d lib",
"start": "node --use_strict ./lib/index.js",
"watch": "nodemon --exec \"npm run build && node lib/index.js\" -e js --ignore lib/"
}
然后运行npm run watch
。之后,每次修改源代码(.js
个文件)时,nodemon都会重新构建项目并重启服务器。
--exec
指定在文件更改时您希望 nodemon 执行哪些非节点脚本(也适用于上述节点脚本 node lib/index.js
)。
-e
指定您希望 nodemon 监视的文件扩展名。
--ignore
指定您希望 nodemon 忽略的 files/directories。这个选项对于解决这个问题是必不可少的,因为如果你不指定忽略这个lib/
文件夹,nodemon将无限重启,因为lib/
中的编译文件也是.js
文件。
"scripts": {
"build": "babel src -d lib",
"start": "node --use_strict ./lib/index.js",
"watch": "nodemon lib/index.js --exec npm run build"
}
使用命令 npm run watch
导致以下错误命令 运行:[nodemon] starting "npm lib/index.js run build"
我如何编写一个 nodemon 命令,在重新加载时使用 babel 转译代码并重新加载代码?
您可以简单地 运行 您的代码 babel-node
以避免显式转译。
$ nodemon lib/index.js --exec babel-node --presets=es2015,stage-2
似乎 this is the recommended 将 nodemon
与 babel
结合使用的方法。
请注意,运行宁 --exec
可能 在远程 运行宁你的 development
环境时产生意想不到的副作用你的 localhost
更好的选择是不使用全局安装,而是使用本地安装的包。这也将有助于根据 12 因素应用程序设计与本地机器设置相同的自动化构建。
"scripts": {
"watch": "node ./node_modules/nodemon/bin/nodemon.js"
}
您可以有两个 nodemon,一个用于转译,另一个用于 运行 您的代码。在 package.json 你可以这样做:
"scripts": {
"serve": "nodemon --watch dist/ ./dist/index.js",
"build" : "nodemon --watch src/ --exec babel ./src --out-dir ./dist --source-maps --copy-files"
},
"scripts": {
"build": "babel src -d lib",
"start": "nodemon --exec babel-node lib/index.js",
"serve": "npm run build && node lib/index.js"
}
Serve 用于生产环境,使用 npm start 你所做的首先是转译,然后 运行 nodemon。
有一个选项可以在 "watch" 模式下使用 Babel 构建文件,让 Nodemon 仅监视 "build" 文件夹并在编译输出发生更改时重新启动应用程序。
{
"name": "app",
"version": "1.0.0",
"private": true,
"dependencies": {},
"devDependencies": {
"@babel/cli": "^7.6.0",
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"nodemon": "^1.19.2"
},
"scripts": {
"build": "babel src --out-dir build --source-maps=inline --verbose",
"start": "yarn build --watch & sleep 1 && nodemon --watch build build/index.js"
}
}
此示例取自 GitHub 上的 GraphQL API Examples
存储库。
"scripts": {
"build": "babel src -d lib",
"start": "node --use_strict ./lib/index.js",
"watch": "nodemon --exec \"npm run build && node lib/index.js\" -e js --ignore lib/"
}
然后运行npm run watch
。之后,每次修改源代码(.js
个文件)时,nodemon都会重新构建项目并重启服务器。
--exec
指定在文件更改时您希望 nodemon 执行哪些非节点脚本(也适用于上述节点脚本 node lib/index.js
)。
-e
指定您希望 nodemon 监视的文件扩展名。
--ignore
指定您希望 nodemon 忽略的 files/directories。这个选项对于解决这个问题是必不可少的,因为如果你不指定忽略这个lib/
文件夹,nodemon将无限重启,因为lib/
中的编译文件也是.js
文件。