如何修复 NPM 模块的 `sh: ___: command not found` [英] How to fix `sh: ___: command not found` for NPM Modules

查看:394
本文介绍了如何修复 NPM 模块的 `sh: ___: command not found`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Node 开发比较陌生,正在尝试各种模块、框架等.我使用的是 macOS Catalina.我在尝试让事情正常工作时更改了我的 NPM 根目录和 $PATH,我想我破坏了一些东西,尽管我不确定是什么.我的问题特别与 NPM 相关,它在本地和全局运行已安装的模块时一直存在问题.当我尝试安装像 Nodemon 这样的开发工具或像 Electron 这样的框架时,我得到如下错误代码:

I'm relatively new to Node development and I'm trying out various modules, frameworks, etc.. I'm on macOS Catalina. I've changed my NPM root and $PATH while trying to get things to work, and I think I broke something, though I'm not exactly sure what. My issue is very specifically with NPM, which is having a persistent problem running installed modules both locally and globally. When I try to install a dev tool like Nodemon or a framework like Electron, I get error codes like:

sh: electron: command not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! electron-quick-start@1.0.0 start: `electron .`
npm ERR! spawn ENOENT
npm ERR! 
npm ERR! Failed at the electron-quick-start@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我在本网站上查阅了大量资源和许多问题,但我的经验不足,无法准确解决此问题并确定发生了什么.当我在黑暗中拍摄时,我可能正在修复一件事并破坏另一件事,所以我来这里看看我是否能得到一个全面的答案.

I've consulted a lot of resources and a lot of questions on this site, but I'm not experienced enough to accurately troubleshoot this issue and identify what's going on. While I'm shooting in the dark, I may be fixing one thing and breaking another, so I'm coming here to see if I can get a comprehensive answer.

我会补充一点,我找到了一些解决方法,如果这能让问题更清晰的话.我可以通过将此dev"脚本添加到 package.json 来运行一些模块(在此示例中为 Nodemon):

I'll add that I've found a few workarounds, if this makes the issue clearer at all. I can get some modules (in this example Nodemon) to run by adding this "dev" script to the package.json:

"scripts": {
    "dev": "node ./node_modules/.bin/nodemon server.js"
  }

所以我可以通过指定模块的路径来访问本地项目模块,但我不能只说nodemon server.js",无论是在开发脚本中还是在命令行中,它应该已经全局安装.如果我查看指定的 npm -g root 文件夹,我尝试运行的包肯定在那里,所以这不是安装问题.

So I can access local project modules by specifying the path of the Module, but I can't just say "nodemon server.js", either in the dev script or from the command line, where it should have been installed globally. If I look in the specified npm -g root folder, the packages I'm trying to run are definitely in there, so it's not an installation issue.

非常感谢你们的任何指导,因为我不知道该怎么做.

Any guidance y'all have is greatly appreciated, as I don't know what to make of all this.

推荐答案

npm 上有 2 种安装方式,本地和全局.
强烈建议不要在同一环境中混合使用两者,至少对于给定的模块是这样.

There are 2 installation style, local and global, on npm.
It is highly recommended to not mix both on the same environment, at least for a given module.

例如npm install -g <模块名称>

您安装的模块提供的可执行文件将被添加到PATH环境变量包含的位置.
然后你可以在任何地方使用这个命令(这里是 electronnodemon),但你不能为你环境中的每个项目使用不同的版本.您可以使用 nvm 或在容器中更好地隔离此行为.

The executable(s) provided by the module you have installed will be added into the place which PATH environment variable includes.
You can then use this command everywhere (here electron or nodemon), but you cannot use different versions for each project in your environment. You can isolate this behavior with nvm, or better in a container.

例如npm install --save <模块名称>

可执行文件将安装到项目 node_modules 目录中并保存在 package.json 中.
然后,您可以为每个项目使用不同版本的模块,并使用 package.json 轻松管理它.
PATH 环境变量不包含在那里,因此您必须使用 ./node_modules/.bin/,就像您在解决方法中所做的那样,或 $(npm bin)/<命令>.
即使是本地安装,您也可以使用 npm-scripts 中模块提供的任何命令,因为 npm./node_modules/.bin 添加到 PATHnpm run-script 命令之前.

The executable(s) will be installed into the project node_modules directory and saved in the package.json.
You can then use different versions of the module for each project and easily manage it with package.json.
PATH environment variable does not include there, so you have to use ./node_modules/.bin/<command>, as you did in your workaround, or $(npm bin)/<command>.
You can also use any command provided by the module in npm-scripts even if it's a local install because npm adds ./node_modules/.bin to PATH before npm run-script command.

另见:https://docs.npmjs.com/cli/v7/commands/npm-run-script

这篇关于如何修复 NPM 模块的 `sh: ___: command not found`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆