nodejs global installation module cannot be read

王林
Release: 2023-05-23 22:49:06
Original
770 people have browsed it

Preface

With the widespread application of Node.js in Web development, many people have begun to use it as the basis for their own development. However, some novices may encounter some problems when installing and using globally installed modules. This article will solve some problems where globally installed modules cannot be read when using Node.js.

Problem Description

When using Node.js, sometimes we will install some modules globally through npm, such as nodemon, express, etc. However, when opening the command line and trying to use these modules through the command line, we may encounter some problems, as shown below:

nodemon : 在此系统上禁止执行脚本。有关详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkID=135170 中的运行 Policy。
所在位置 行:1 字符: 1
+ nodemon
+ ~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
Copy after login

or

'express' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
Copy after login

This situation means that we installed The module cannot be read from the command line, why?

Cause Analysis

This situation is usually caused by incorrect configuration of environment variables. Globally installed modules cannot be read on the command line, possibly because Node.js cannot find the relevant installation files.

When Node.js uses npm to install modules, the installed modules will be stored in the local folder by default instead of the global path. When we execute the module in the command line, Node.js will look for the path to store the module. If the path is not in the PATH environment variable of Node.js, a read failure will occur.

Solution

In order to solve this problem, we need to perform the following steps:

  1. Confirm the global module path of Node.js

First, we need to find out the path to the Node.js global module. This can be viewed on the command line using the following code:

npm root -g
Copy after login

This command will return the path to the Node.js global module, for example:

C:UsersUsernameAppDataRoaming
pm
ode_modules
Copy after login
Copy after login
  1. Add the Node.js global module path to In the environment variables

Open the control panel on your computer, find and open the "Environment Variables" option, as shown below:

nodejs global installation module cannot be read

In the "Environment Variables" window, find the "Path" option under "System Variables" and click the "Edit" button. In the Edit Environment Variables window, click the New button and copy-paste the Node.js global module path into the option:

C:UsersUsernameAppDataRoaming
pm
ode_modules
Copy after login
Copy after login
  1. Restart command line

After adding the environment variables, close and reopen the command line, or execute the following command in the opened command line to update the environment variables:

refreshenv
Copy after login

Now, we can try to install the module globally again. For example, execute the following code to start nodemon:

nodemon
Copy after login

Or start the express application:

express myapp
Copy after login

Conclusion

This article solves the problem that the global installation module cannot be read when using Node.js The problem. If you are developing with Node.js, it is recommended to follow the above steps to solve similar problems. If you have any questions or need help, please leave a message in the comment area.

The above is the detailed content of nodejs global installation module cannot be read. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template