简体   繁体   English

Node npm package throw use strict: 在全局发布和安装后找不到命令

[英]Node npm package throw use strict: command not found after publish and install globaly

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?当我在本地开发中运行它时,它运行良好,有什么问题?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM