节点npm包抛出use strict:发布并安装globaly后找不到命令

时间:2021-06-10 23:00:01

I am trying to publish npm package, when i am install the package globally and try to run the cli command i get this errors:

我试图发布npm包,当我全局安装包并尝试运行cli命令时我得到这个错误:

/.nvm/versions/node/v0.12.2/bin/myPack: line 1: use strict: command not found
/.nvm/versions/node/v0.12.2/bin/myPack: line 3: syntax error near unexpected token `('
/.nvm/versions/node/v0.12.2/bin/myPack: line 3: `var _commandLineArgs = require('command-line-args');'

The top of the file that the error refer to:

错误引用的文件顶部:

'use strict';

var _commandLineArgs = require('command-line-args');

var _commandLineArgs2 = _interopRequireDefault(_commandLineArgs);

The package.json bin section:

package.json bin部分:

  "bin": {
    "myPack": "dist/myPack.js"
  }

When i am running this in my local development this works well, what is the problem?

当我在我的本地开发中运行它时效果很好,问题是什么?

1 个解决方案

#1


36  

Your script should start with a shebang line, otherwise it will be executed as a shell script (hence the errors).

你的脚本应该以shebang行开头,否则它将作为shell脚本执行(因此错误)。

Add this as first line to dist/myPack.js:

将此作为第一行添加到dist / myPack.js:

#!/usr/bin/env node

#1


36  

Your script should start with a shebang line, otherwise it will be executed as a shell script (hence the errors).

你的脚本应该以shebang行开头,否则它将作为shell脚本执行(因此错误)。

Add this as first line to dist/myPack.js:

将此作为第一行添加到dist / myPack.js:

#!/usr/bin/env node