Skip to content

Can't use 'script' post action to run 'npm install' #849

Closed
@SteveSandersonMS

Description

@SteveSandersonMS

Glad to see the improved postActions support!

I've tried to add this to the ASP.NET Core SPA templates. The postAction for dotnet restore works well, but I've not succeeded in configuring a postAction to run npm install.

With this config:

{
    "description": "Restores NPM packages required by this project.",
    "manualInstructions": [{ "text": "Run 'npm install'" }],
    "actionId": "3A7C4B45-1F5D-4A30-959A-51B88E82B5D2",
    "args": { "executable": "npm", "args": "install" },
    "continueOnError": false
}

... it instantly fails with this error:

Processing post-creation actions...
Template is configured to run the following action:
Description: Restores NPM packages required by this project.
Manual instructions: Run 'npm install'
Actual command: npm install
Do you want to run this action (Y|N)?
y
Running command 'npm install'...
The system cannot find the file specified

... even though running npm install manually in the same command line works fine.

I think this is because, on Windows, npm is installed as a batch file called npm.cmd (not npm.exe).

If I change the config to "args": { "executable": "npm.cmd", args: "install" } (which is obviously not a cross-platform solution), then it gets a little further, but then fails with:

Processing post-creation actions...
Template is configured to run the following action:
Description: Restores NPM packages required by this project.
Manual instructions: Run 'npm install'
Actual command: npm.cmd install
Do you want to run this action (Y|N)?
y
Running command 'npm.cmd install'...
Command failed.
Output from command:
module.js:471
    throw err;
    ^

Error: Cannot find module 'C:\path\to\yourproject\node_modules\npm\bin\npm-cli.js'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:390:7)
    at startup (bootstrap_node.js:150:9)
    at bootstrap_node.js:505:3
module.js:471
    throw err;

Again, this doesn't occur when running npm.cmd install manually, so this is a problem to do with how the process is being launched.

As a further test, I tried changing the config to "args": { "executable": "cmd", args: "/c \"npm install\"" } (which again is obviously not a cross-platform solution, but this is just for debugging). In this case it does succeed with running npm install, but it's not practical as a solution because:

  • It's not cross-platform
  • It completely hides npm's console output, so there's no progress indication. Since npm install can take minutes to complete, seeing the progress output is essential.

Possible solutions

  1. Maybe you could fix this by changing this line to set UseShellExecute to true - not sure. If you do this, please make sure the command's console output is passed through and visible to the user.
  2. Better solution Have an explicit built-in post action for npm install, considering what a mainstream use case this is. This is better, because then you wouldn't have to throw up the scary permissions prompt. It would feel a lot more streamlined.

Do you think it will be possible to resolve this before preview2 ships? We're planning to have SPA templates in the box in preview 2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions