Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands that involve npm are taking too long when behind proxy #3683

Closed
n0v1 opened this issue Jun 14, 2018 · 4 comments
Closed

Commands that involve npm are taking too long when behind proxy #3683

n0v1 opened this issue Jun 14, 2018 · 4 comments

Comments

@n0v1
Copy link

n0v1 commented Jun 14, 2018

Please, provide the details below:

Did you verify this is a real problem by searching the NativeScript Forum and the other open issues in this repo?

yes

Tell us about the problem

When you are behind a corporate proxy and you execute NativeScript CLI commands like tns doctor that in turn executes npm commands like npm -v in a child process, those commands take far too long to finish. tns doctor for example runs for 3 minutes until the results are shown.

This seems to happen on Windows systems only.

Debugging showed that the cause of this problem is the update check feature that was introduced in npm 4.4.0. The update-notifier package that is used for this does not support proxies. It spawns a deferred child process that prevents the parent process (the one that executes npm -v) from closing on some platforms.

See this call in nativescript-doctor sys-info: https://github.com/NativeScript/nativescript-doctor/blob/81fe42a2cd63812b138bcd579ecbb384d83bcf0d/lib/sys-info.ts#L99

The callback here will be called only after the process closes. On windows the close event of this child process will be fired after the update check process is completed. As it does not support proxies, it times out after 3 minutes with a unhandled promise rejection (that does not hit the surface) and so the script execution halts here for this 3 minutes.

This problem occurs with all npm version from 4.4.0 up.

A workaround could be to set the environment variable NO_UPDATE_NOTIFIER for the CLI process (and thus for all child processes it spawns). See package description.

Please provide the following version numbers that your issue occurs with:

  • CLI: 4.1.0
  • Cross-platform modules: 3.4.1
  • Runtime(s): tns-android@4.1.2
  • Plugin(s): nativescript-barcodescanner, nativescript-webview-interface, nativescript-vue

Please tell us how to recreate the issue in as much detail as possible.

Please see this repository

Is there code involved? If so, please share the minimal amount of code needed to recreate the problem.

const childProcess = require('child_process')

const options = {
  timeout: 5000,
  // env: {
  //   NO_UPDATE_NOTIFIER: true
  // }
}

console.log(new Date(Date.now()).toLocaleString(), '- START')
console.time('childProcess.exec')

const child = childProcess.exec('npm --version', options, (err) => {
  console.timeEnd('childProcess.exec')
  if (err) {
    console.error(`Error: ${err}`)
    return
  }
})

child.stdout.on('data', (data) => {
  console.log(new Date(Date.now()).toLocaleString(), '- DATA -', 'npm version:', data.split('\n')[0])
})

child.on('error', (err) => {
  console.log(new Date(Date.now()).toLocaleString(), '- ERROR -', err)
})

child.on('exit', (code) => {
  console.log(new Date(Date.now()).toLocaleString(), '- EXIT -', code)
  // const activeHandles = process._getActiveHandles()
  // const activeRequests = process._getActiveRequests()
  // console.log('active handles:', activeHandles.length, 'active requests:', activeRequests.length)
})

child.on('close', (code) => {
  clearInterval(intervalId)
  console.log(new Date(Date.now()).toLocaleString(), '- CLOSE -', code)
})

Expected output:

2018-6-13 13:15:32 - START
2018-6-13 13:15:33 - DATA - npm version: 5.6.0
2018-6-13 13:15:33 - EXIT - 0
childProcess.exec: 934.978ms
2018-6-13 13:15:37 - CLOSE - 0

Actual output without an expicit timeout:

2018-6-13 13:15:32 - START
2018-6-13 13:15:33 - DATA - npm version: 5.6.0
2018-6-13 13:15:33 - EXIT - 0
childProcess.exec: 189416.095ms
2018-6-13 13:18:31 - CLOSE - 0

Actual output with an timeout of 10 seconds:

2018-6-13 10:32:34 - START
2018-6-13 10:32:35 - DATA - npm version: 5.6.0
2018-6-13 10:32:35 - EXIT - 0
childProcess.exec: 10019.768ms
2018-6-13 10:32:43 - CLOSE - 0

Actual output equals expected output when adding environment variable NO_UPDATE_NOTIFIER to the exec options.

Related issues

@n0v1
Copy link
Author

n0v1 commented Jul 6, 2018

npm 6.2 seems to include an option to disabled the update check via --no-update-notifier or --update-notifier=false.
See npm/npm#20750 and https://npm.community/t/npm-side-config-for-suppressing-update-notifier/75/5

@rosen-vladimirov
Copy link
Contributor

Hey @n0v1 ,
Have you set the npm proxy before executing these commands - by default npm does not respect the system proxy, so you have to set it manually:

npm config set proxy <your proxy url>
npm config set https-proxy <your proxy url>

For example:

npm config set proxy http://localhost:8888
npm config set https-proxy http://localhost:8888

@n0v1
Copy link
Author

n0v1 commented Aug 3, 2018

Yes, I've set the npm proxy settings from day one. npm works fine when using it "normally". Disabling the update check by setting the environment variable NO_UPDATE_NOTIFIER fixes the problem for me. It persists otherwise.

@rosen-vladimirov
Copy link
Contributor

Okay, thanks for the information. As the issue is related to npm itself not NativeScript specifically, I'm closing it.
Thanks a lot for sharing the workaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants