ZhangZhihui's Blog  

 

PS D:\VSCodeWorkspace\Git\LangChain> npm init -y
npm : 无法加载文件 D:\Program Files\nodejs\npm.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ npm init -y
+ ~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

 

这是 Windows PowerShell 的 执行策略(Execution Policy) 限制导致的。默认情况下,PowerShell 阻止运行 .ps1 脚本,这是为了安全防护。

你可以按以下步骤解决:


方法 1:临时允许执行脚本

在 PowerShell 中运行:

Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
  • -Scope Process 表示只对当前 PowerShell 会话有效,关闭窗口后失效。

  • 然后再运行:

npm init -y

方法 2:永久修改执行策略

如果你希望永久允许:

  1. 管理员身份打开 PowerShell。

  2. 运行:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
  • RemoteSigned 意味着本地脚本可以运行,下载的脚本必须有签名。

  • 然后再运行:

npm init -y

方法 3:直接用 cmd 或 Windows Terminal

如果不想修改策略,可以直接在 命令提示符 (cmd)Windows Terminal(切换到 cmd 模式) 中运行:

npm init -y

💡 小贴士:执行策略是 PowerShell 的安全机制,通常不建议永久关闭。如果只是临时使用 npm,方法 1 就够了。

 

PS D:\VSCodeWorkspace\Git\LangChain> npm init -y
Wrote to D:\VSCodeWorkspace\Git\LangChain\package.json:

{
  "name": "langchain",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "commonjs"
}

 

PS D:\VSCodeWorkspace\Git\LangChain> npm install langchain @langchain/core

added 33 packages, and audited 34 packages in 15s

9 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
PS D:\VSCodeWorkspace\Git\LangChain> npm install typescript tsx @types/node zod

added 8 packages, and audited 42 packages in 11s

11 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
PS D:\VSCodeWorkspace\Git\LangChain> npm install @langchain/openai

added 2 packages, and audited 44 packages in 6s

11 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
PS D:\VSCodeWorkspace\Git\LangChain> npm install @langchain/anthropic

added 5 packages, and audited 49 packages in 6s

11 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

 

package.json:

{
  "name": "langchain",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "type": "commonjs",
  "dependencies": {
    "@langchain/anthropic": "^1.2.3",
    "@langchain/core": "^1.1.4",
    "@langchain/openai": "^1.1.3",
    "@types/node": "^24.10.1",
    "langchain": "^1.1.5",
    "tsx": "^4.21.0",
    "typescript": "^5.9.3",
    "zod": "^4.1.13"
  }
}

 

If all the dependencies are already in the package.json, running 'npm install' can install them all.

posted on 2025-12-08 16:34  ZhangZhihuiAAA  阅读(1)  评论(0)    收藏  举报