节点项目在本地工作但在通过NPM安装时失败?

时间:2016-11-04 07:54:44

标签: javascript node.js npm npm-install

我有这个简单node project,当我在本地运行main.js文件node main.js时,它运行正常。但是,当我执行npm install -g cli-tweet然后尝试运行它时,它会输出此错误:

/home/USER/.npm-global/bin/tweet: 1: /home/USER/.npm-global/bin/tweet: Syntax error: "(" unexpected

package.json看起来像这样:

{
"name": "cli-tweet",
"main": "main.js",
"bin": {
"tweet": "main.js"
},
[...]
}

有关如何解决此问题的任何想法?

编辑1:

main.js的代码

var OAuth = require('oauth').OAuth,
    colors = require('colors'),
    Twitter = require('twitter'),
    fs = require('fs'),
    get_args = require('cli-pipe');

var CONFIG_FILE = '.tweet.json',
    REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token',
    ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token',
    OAUTH_VERSION = '1.0', HASH_VERSION = 'HMAC-SHA1';

var key = "XYZ",
    secret = "XYZ", tweetText;

function getAccessToken(oa, oauth_token, oauth_token_secret, pin) {
    oa.getOAuthAccessToken(oauth_token, oauth_token_secret, pin,
        function (error, oauth_access_token, oauth_access_token_secret, results2) {
            if (error && parseInt(error.statusCode) == 401) {
                throw new Error('The pin number you have entered is incorrect'.bold.red);
            }
            var keys = {
                'ACCESS_TOKEN_KEY': oauth_access_token,
                'ACCESS_TOKEN_SECRET': oauth_access_token_secret
            };
            fs.open(CONFIG_FILE, "wx", function (err, fd) {
                try {
                    fs.close(fd, function (err) {
                    });
                } catch (e) {
                }
            });
            fs.writeFileSync(CONFIG_FILE, JSON.stringify(keys));
            console.log('Try echo "test" | cli-tweet'.cyan);
            process.exit(1);
        });
}

function getRequestToken(oa) {
    oa.getOAuthRequestToken(function (error, oauth_token, oauth_token_secret, results) {
        if (error) {
            throw new Error(([error.statusCode, error.data].join(': ')).bold.red);
        } else {
            console.log(('https://twitter.com/oauth/authorize?oauth_token=' + oauth_token).underline.blue)
            console.log('Enter the pin number here:'.bold.yellow);
            var stdin = process.openStdin();
            stdin.on('data', function (chunk) {
                var pin = chunk.toString().trim();
                getAccessToken(oa, oauth_token, oauth_token_secret, pin);
            });
        }
    });
}

function tweet(userTokens) {
    var client = new Twitter({
        consumer_key: key,
        consumer_secret: secret,
        access_token_key: userTokens.oauth_access_token,
        access_token_secret: userTokens.oauth_access_token_secret
    });
    console.log("Tweet :" + tweetText.bold.cyan);
    if (tweetText.length > 0) {
        client.post('statuses/update', {status: tweetText}, function (error, tweet, response) {
            if (error) {
                console.log("Error :" + JSON.stringify(error));
            }
            process.exit(1);
        });
    } else {
        console.log("Pipe a tweet".bold.red);
    }
}

var isConfig = process.argv[2];
if (isConfig === undefined || isConfig.toLowerCase() != "config") {
    try {
        var contents = fs.readFileSync(CONFIG_FILE).toString(), tokens = JSON.parse(contents);
    } catch (e) {
        console.log("Error: Try running 'tweet config' command".bold.red);
    }
    if (tokens != undefined && (tokens.ACCESS_TOKEN_KEY != undefined && tokens.ACCESS_TOKEN_SECRET != undefined)) {
        try {
            get_args(function (args) {
                tweetText = args[2];
                tweet({
                    "oauth_access_token": tokens.ACCESS_TOKEN_KEY,
                    "oauth_access_token_secret": tokens.ACCESS_TOKEN_SECRET
                });
            });
        } catch (e) {
            console.log("Error: Unexpected error while tweeting".bold.red);
        }
    } else {
        console.log("Try running 'cli-tweet config' command".bold.red);
    }
} else {
    var oa = new OAuth(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, key, secret, OAUTH_VERSION, 'oob', HASH_VERSION);
    getRequestToken(oa);
}

编辑2:

运行如下代码:node /home/USER/.npm-global/lib/node_modules/cli-tweet/main.js似乎有效

1 个答案:

答案 0 :(得分:1)

1)添加文件夹bin

2)使用

添加文件bin/tweet
#!/usr/bin/env node
require('../main.js');

3)更改package.json

"bin": {
    "tweet": "./bin/tweet",
}