gpt4 book ai didi

javascript - NPM脚本问题

转载 作者:行者123 更新时间:2023-12-02 18:58:16 25 4
gpt4 key购买 nike

我有一个nodejs文件runner.node.js

如果我运行node runner.node.js它就可以工作

但是如果我尝试使用 npm test 运行它(它在 package.json 中引用):

"test": "node ./spec/runner.node.js"

"test": "spec/runner.node.js"

它说该文件不可执行:

sh: 1: spec/runner.node.js: Permission denied
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0

如果我将文件设置为可执行文件,则会显示:

spec/runner.node.js: 1: spec/runner.node.js: Syntax error: word unexpected (expecting ")")
npm ERR! Test failed. See above for more details.
npm ERR! not ok code 0

虽然它仍然可以使用“node spec/runner.node.js”正确运行

文件是这样的:

console.log("Running Knockout tests in Node.js");

var fs = require('fs');
var jasmine = require('./lib/jasmine-1.2.0/jasmine');

// export jasmine globals
for (var key in jasmine) {
global[key] = jasmine[key];
}

// add our jasmine extensions to the exported globals
require('./lib/jasmine.extensions');

// export ko globals
if (process.argv.length > 2 && process.argv[2] == '--source') {
// equivalent of ../build/knockout-raw.js
global.DEBUG = true;
global.ko = global.koExports = {};
global.knockoutDebugCallback = function(sources) {
sources.unshift('build/fragments/extern-pre.js');
sources.push('build/fragments/extern-post.js');
eval(sources.reduce(function(all, source) {
return all + '\n' + fs.readFileSync(source);
}, ''));
};
require('../build/fragments/source-references');
} else {
global.ko = require('../build/output/knockout-latest.js');
}

// reference behaviors that should work out of browser
require('./arrayEditDetectionBehaviors');
require('./asyncBehaviors');
require('./dependentObservableBehaviors');
require('./expressionRewritingBehaviors');
require('./extenderBehaviors');
require('./mappingHelperBehaviors');
require('./observableArrayBehaviors');
require('./observableBehaviors');
require('./subscribableBehaviors');

// get reference to jasmine runtime
var env = jasmine.jasmine.getEnv();

// create reporter to return results
function failureFilter(item) {
return !item.passed();
}
env.addReporter({
reportRunnerResults:function (runner) {
var results = runner.results();
runner.suites().map(function (suite) {
// hack around suite results not having a description
var suiteResults = suite.results();
suiteResults.description = suite.description;
return suiteResults;
}).filter(failureFilter).forEach(function (suite) {
console.error(suite.description);
suite.getItems().filter(failureFilter).forEach(function (spec) {
console.error('\t' + spec.description);
spec.getItems().filter(failureFilter).forEach(function (expectation) {
console.error('\t\t' + expectation.message);
});
});
});
console.log("Total:" + results.totalCount + " Passed:" + results.passedCount + " Failed:" + results.failedCount);
process.exit(results.failedCount);
}
});

// good to go
env.execute();

最佳答案

添加

#/usr/bin/env node

作为文件中的第一行。这样,当作为可执行文件运行时,您的操作系统将知道它将使用 Node.js 来运行它(确切地说:您的操作系统将知道它将使用第一个名为 node 的应用程序来执行您的脚本)。

关于javascript - NPM脚本问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15090339/

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