gpt4 book ai didi

node.js - npm:从项目文件夹内的任何位置运行 package.json 中的脚本

转载 作者:IT老高 更新时间:2023-10-28 23:12:56 28 4
gpt4 key购买 nike

我正在使用 package.json 文件中的 scripts 部分来存储一些我必须定期运行的命令。

 "scripts": {
"test": "./test/phantomjs ./test/js/run_jasmine_test.coffee ./test/index.html",
"rjs": "r.js -o ./js/app.build.js",
"less": "lessc -x ./css/app.less > ./css/app.css"
}

在每个命令中,我在路径的开头都有一个 ./ - 这就是为什么我只能从项目的根目录调用 npm run-script rjs .有没有办法在 package.json 中引用项目的根目录,以便我可以运行例如npm test 从我项目的任何地方?

最佳答案

我认为这取决于您组织项目的方式。

我创建了一个示例项目只是为了演示。这是我的示例项目目录树:

.
├── dir1
│   └── dir2
├── package.json
├── src
│   └── index.js
└── test
└── test.js

注意: ./dir1/dir2 只是用于演示的两个空目录。

我的 package.json 文件如下所示:

{
"name": "sample",
"version": "1.0.0",
"description": "sample",
"main": "index.js",
"scripts": {
"test": "node test/test.js",
"start": "node src/index.js"
},
"author": "yaniv",
"license": "ISC"
}

专注于脚本部分,这两行很简单,无需考虑我的位置。

如果我将目录更改为 /dir1/dir2 并执行 npm test 并且我在 test 属性上编写的脚本在没有./ 的使用(定义当前目录...)

底线 - 我想如果你重新组织你的项目并删除 ./ 将在你的情况下完成这项工作。

关于node.js - npm:从项目文件夹内的任何位置运行 package.json 中的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13860497/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com