【问题标题】:sharing NPM script/app through azure devops organizations using azure artifacts使用 azure artifacts 通过 azure devops 组织共享 NPM 脚本/应用程序
【发布时间】:2021-05-18 15:29:24
【问题描述】:

我有一个 git 存储库,其中包含一个 node.js“应用程序”(只有 1 个文件)及其 package.json。

我需要使用来自其他组织的 azure devops 管道的“应用程序”,因此我在该 nodejs 项目中创建了一个管道,该管道在基于 NPM 的 azure artifact feed 上对该脚本进行版本控制和发布。

在另一个组织中,在我想使用该应用程序的管道中,我使用了这样配置的“npm”任务:

steps:
- task: Npm@1
  displayName: 'npm install MyPackage'
  inputs:
    command: custom
    verbose: false
    customCommand: 'install MyPackage'
    customRegistry: useFeed
    customFeed: '...some-guid....'

安装似乎没问题(我在第二个组织中创建了一个提要,其中第一个组织的提要作为文档中所示的上游源)。

现在,我应该如何在下一个管道任务中执行该脚本? 我试图连接到代理,但是当我运行“MyPackage”命令时,我得到“找不到命令”。 脚本安装在哪里?我应该如何执行它? PATH 中有什么要放的吗? 我应该使用“-g”选项吗?

感谢您的帮助:)

使用请求的日志进行更新:

任务日志:

Starting: npm custom
==============================================================================
Task         : npm
Description  : Install and publish npm packages, or run an npm command. Supports npmjs.com and authenticated registries like Azure Artifacts.
Version      : 1.182.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/package/npm
==============================================================================
/usr/local/bin/npm --version
6.13.7
/usr/local/bin/npm config list
; cli configs
metrics-registry = "https://pkgs.dev.azure.com/org/_packaging/....some-guid..../npm/registry/"
scope = ""
user-agent = "npm/6.13.7 node/v13.11.0 darwin x64"

; environment configs
userconfig = "/Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/10/npm/33366.npmrc"

; userconfig /Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/10/npm/33366.npmrc
registry = "https://pkgs.dev.azure.com/org/_packaging/....some-guid..../npm/registry/"

; builtin config undefined
prefix = "/usr/local"

; node bin location = /usr/local/Cellar/node/13.11.0/bin/node
; cwd = /Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/10/s
; HOME = /Users/...
; "npm config ls -l" to show all defaults.

/usr/local/bin/npm install -g MyPackage
+ MyPackage@1.0.20210216-1
added 198 packages from 99 contributors in 18.319s
Finishing: npm custom

这是第二个定义:

steps:
- script: |
   echo "1"
   node MyPackage
   
   echo "2"
   MyPackage
   
  displayName: 'Command Line Script'

以及相关的执行日志:

Starting: Command Line Script
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.182.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/bin/bash --noprofile --norc /Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/_temp/7fe7833a-ce7d-48af-ab8f-1fcf2e740c36.sh
1
internal/modules/cjs/loader.js:979
  throw err;
  ^

Error: Cannot find module '/Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/10/s/MyPackage'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:976:15)
    at Function.Module._load (internal/modules/cjs/loader.js:859:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
2
/Users/.../AGENTS/vsts-agent-osx-x64-2.179.0-AZURE-01/_work/_temp/7fe7833a-ce7d-48af-ab8f-1fcf2e740c36.sh: line 5: MyPackage: command not found
##[error]Bash exited with code '127'.
Finishing: Command Line Script

【问题讨论】:

    标签: node.js azure-devops npm-install azure-artifacts


    【解决方案1】:

    终于找到问题了:

    • 首先我必须使用-g 选项全局安装该软件包
    • 第二个我不得不使用npx 运行它。
    • 第三,我必须更新package.json
      "bin": {
        "NAME_OF_THE_BINARY": "./FILE_TO_EXECUTE.js"
      },
    

    【讨论】:

    • 您好,您可以随时接受您的回答。谢谢
    【解决方案2】:

    我们可以add an Azure Artifacts feed in a different organization within your Azure AD tenant as an upstream source

    比如你在第一个组织中创建了一个feed,并且在feed中添加了npm包,现在你想在第二个组织中使用这个包,我们需要在第二个组织的feed中设置上游源,比如azure-feed://myOrg/myProject/myFeed@local,那么我们就可以在第二个组织管道中安装和使用该包了。

    您也可以查看blog 了解更多详情。

    更新1

    我需要找到它的安装路径

    我们可以添加任务Command line 并运行npm list 来查看您当前位置已安装的非全局库。查看ticketblog 了解更多详情。

    安装路径为$(Build.SourcesDirectory)/node_modules,我们可以添加task bash并运行ls '$(Build.SourcesDirectory)/node_modules'查看。

    【讨论】:

    • when I run "MyPackage" command, I get "command not found" 你能在这里分享详细日志和任务定义吗?您也可以查看此ticket 进行故障排除
    • 我已经分享了 NPM 安装部分的任务定义。我的可执行文件/包的安装似乎没问题,即使跨组织,这不是我的要求,因为这部分有效。问题是运行“包”。我不知道它安装在哪里。我认为解决方案的一部分是在我的 .js 文件顶部添加“#!/usr/bin/env node”,以便能够在前面没有“node”的情况下执行。但是我仍然需要找到它的安装路径...
    • 嗨@LiohAu,我已经更新了答案,检查update1,我们可以运行cmd npm list 来查看安装路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 2021-11-15
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    相关资源
    最近更新 更多