安装Node.js后,npm失败 [英] npm failing right after installing Node.js

查看:213
本文介绍了安装Node.js后,npm失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动安装Node.js的安装程序,将节点应用程序提取到文件夹,然后通过 npm install 安装。但是,安装程序总是需要运行两次才能使 npm 工作。这是安装到Windows上。



在我的安装程序应用程序的一生中...




  • 如果Node.js尚未安装,则安装

  • 在相同的过程中,稍后再步骤 npm install 执行

  • 如果Node.js在安装程序启动之前不存在,则说明 npm 不是有效的命令

  • 如果安装程序启动时已经安装了Node.js,则成功



我假设是否安装 Node.js 创建新的环境变量,但是我的进程还没有获得这些新的变量 - 直到重新启动该过程。那么,第二次可以看到那些变量。否则,在相同的过程中,它不能找到 npm ,因为它不能看到新的环境变量。至少这是我把问题缩小到了,是唯一的解释为什么我总是要运行我的安装程序两次。



背景:安装程序是使用Inno Setup(Unicode)。我在Delphi中创建了一个应用程序,以便在安装完成时向用户显示一个界面,因为大多数原始安装程序只显示一个空白的无响应页面,其中显示准备安装...。该Delphi应用程序在线程内部执行实际的安装过程,该线程使用事件来更新用户可见的网格。此事件驱动的线程将每个安装步骤的进度报告回主窗体,并显示响应式用户界面,显示安装过程的每个步骤及其状态。然后,此应用程序将退出代码返回给安装程序进行进一步处理。



如果不需要等待每个子进程并获取退出代码,这不是一个问题。但是,由于我需要这样做,Windows自然地将缓存的环境变量传递给每个子进程,仍然使它们无用。



如何强制我的应用程序>请参阅从进程开始以来添加的新的环境变量?

解决方案

解决方案不需要任何重新启动的应用程序是将完整路径传递到 nodejs 程序文件夹中的 npm 命令。这样你就不需要依赖于Windows和它的环境变量 - 这是一条直接的路线。请注意您是否正在安装32位或64位版本的Node.js,并确保查看相应的文件夹...

  C:\Program Files\\\
odejs\\\
pm
C:\Program文件(x86)\\\
odejs\\\
pm


I'm working on an installer which automates the installation of Node.js, extracts a node application to a folder, and then installs it via npm install. However, the installer always needs to be run twice in order for npm to work. This is installing onto Windows.

During the lifetime of my installer app...

  • If Node.js wasn't already installed, it gets installed
  • In the same process, steps later, npm install is executed
  • If Node.js didn't already exist before installer started, it fails saying npm is not a valid command
  • If Node.js was already installed when installer started, it succeeds

What I'm assuming is that the installation of Node.js creates new environment variables, but my process has not yet obtained these new variables - until the process is restarted. Then, the second time it can see those variables. Otherwise, within the same process, it cannot find npm because it cannot see the new environment variables. At least this is what I've narrowed the issue down to and is the only explanation why I always have to run my installer twice.

Background: The installer is created using Inno Setup (Unicode). I created an application in Delphi to display the user an interface while the installation is being done, as a majority of the original installer just displayed a blank non-responsive page saying "Preparing to install...". This Delphi application performs the actual install process inside of a thread, and that thread uses events to update a grid visible to the user. This event-driven thread reports the progress of each installation step back to the main form, and displays a responsive user interface showing each step of the install process and its status. This application then returns back an exit code to the installer for further handling.

If it weren't for requiring the wait for each sub-process and obtaining their exit codes, this wouldn't be an issue. But since I need to do that, Windows naturally passes the cached environment variables to each sub-process, still rendering them useless.

How can I force my application to see the new environment variables which have been added since the process started?

解决方案

The solution without requiring any restart of your application is to pass the full path to the npm command in the nodejs program files folder. That way you won't need to rely on Windows and its environment variables - it's a direct route. Keep note of whether you're installing the 32bit or 64bit editions of Node.js, and make sure you look in the appropriate folder...

C:\Program Files\nodejs\npm
C:\Program Files (x86)\nodejs\npm

这篇关于安装Node.js后,npm失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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