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

browser unable to load latest node version #1824

Open
abid71 opened this issue Apr 2, 2018 · 11 comments
Open

browser unable to load latest node version #1824

abid71 opened this issue Apr 2, 2018 · 11 comments

Comments

@abid71
Copy link

abid71 commented Apr 2, 2018

i'm running my Mocha scripts via Browserify in browser but got the following error in browser's console:

"Uncaught Error: Sync-request requires node version 0.12 or later. If you need to use it with an older version of node
you can npm install sync-request@2.2.0, which was the last version to support older versions of node."

However installed nodejs's version on machine is `v9.5.0'

node_v

@ljharb
Copy link
Member

ljharb commented Apr 2, 2018

Synchronous requests aren’t really possible in the browser (they’re possible but deprecated). Even in node, I’d recommend only doing requests asynchronously.

@abid71
Copy link
Author

abid71 commented Apr 4, 2018

@ljharb

There must be workaround to solve this issue, please try at your end and let me if there's any possibilities?

@goto-bus-stop
Copy link
Member

Which version of sync-request are you using? I can't reproduce this with the most recent version.

@abid71
Copy link
Author

abid71 commented Apr 5, 2018

@goto-bus-stop

had a detailed discussion with @ForbesLindesay on this topic here: ForbesLindesay/sync-request#93

I'm able to figure out the exact issue and im pretty sure about this:
This issue was appearing due to child process named "spawnSync" which don't have support at browser.

spawnsync

@ForbesLindesay
Copy link
Contributor

ForbesLindesay commented Apr 5, 2018

@abidrana7 I think you will start to get better help if you correctly identify the issue you are experiencing. The problem you are having is that the "browser" field in package.json seems to be being ignored. Of course spawnSync isn't available in the browser. spawnSync spawns a separate node.js process, how on earth do you expect to start a real node.js instance in a web browser?

spawn-sync does provide a browser implementation (although @ljharb is quite right that you shouldn't use it in any real applications) so it should work, but there is something very strange about your setup. Maybe you've set the NODE_PATH environment variable or something?

In any case, I strongly recommend that you stop posing your question as a problem of not being able to load node in the browser.

@abid71
Copy link
Author

abid71 commented Apr 5, 2018

@ForbesLindesay apologies, if the title of my each question is disturb you at all. My intention was to just logged an issue of what I got an error message.

Can you give me any idea on who to use spawn-sync keeping in view the current scenario. Meantime, I'm working on to replace Async request with sync-requests.

@goto-bus-stop
Copy link
Member

Can you share how you are calling browserify? If I have a script.js like below:

var sr = require('sync-request')

Doing browserify script.js just works.

@abid71
Copy link
Author

abid71 commented Apr 5, 2018

I'm using grunt-browserify v 5.2.0 and successfully able to generate the bundle file which contain all files's content.

@goto-bus-stop
Copy link
Member

A code snippet showing how you are using it and with which configuration would be helpful.

@abid71
Copy link
Author

abid71 commented Apr 5, 2018

this is my grunt file's content:

`'use strict';

var serverRootUri = 'http://127.0.0.1:8000';
var mochaTestRunner = serverRootUri + '/browser/mocha.html';

module.exports = function(grunt) {

// configure grunt
grunt.initConfig({

	pkg: grunt.file.readJSON('package.json'),
	jshint: {
		files: [
			'**/*.js',
			'!node_modules/**/*',
		],
		options: {
			jshintrc: '.jshintrc'
		}
	},

	// run the mocha tests via Node.js
	mochaTest: {
		test: {
			options: {
				reporter: 'mocha-allure-reporter'
			},
			src: ['./specs/back-end/properties/dy_properties.spec.js']
		}
	},
	clean: {
		dist: ['./browser/dist/**/*'],
		//tests: ['./browser/test/browserified_tests.js'],
	},
	browserify: {
		standalone: {
			src: [ 'specs/back-end/properties/dy_properties.spec.js' ],
			dest: './browser/dist/<%= pkg.name %>.standalone.js',
			options: {
				browserifyOptions: {
					standalone: '<%= pkg.name %>'
				}
			}
		},
		tests: {
			src: [ 'specs/back-end/properties/dy_properties.spec.js' ],
			dest: './browser/test/browserified_tests.js',
			options: {
				external: [ './<%= pkg.name %>.js' ],
				// Embed source map for tests
				debug: true
			}
		}
		/*require: {
			src: [ 'specs/back-end/properties/dy_properties.spec.js' ],
			dest: './browser/dist/<%= pkg.name %>.require.js',
			options: {
				alias: [ './<%= pkg.name %>.js:' ]
			}
		}*/
	},
	// Uglify browser libs
	uglify: {
		dist: {
			files: {
				'browser/dist/<%= pkg.name %>.standalone.min.js':
				['./browser/dist/<%= pkg.name %>.standalone.js'],
				'browser/dist/<%= pkg.name %>.require.min.js':
				['./browser/dist/<%= pkg.name %>.require.js'],
			}
		}
	},
	connect: {
		// Used for mocha-phantomjs tests
		server: {},
		keepalive: {
			options: {
				keepalive: true,
				urls: [
					mochaTestRunner
				]
			}
		}
	},
});

// Load plug-ins
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-connect');

// define tasks
grunt.registerTask('default', [
	//'jshint',
	'clean',
	//'mochaTest',
	'browserify',
	//'uglify',
	'connect:server',
]);

};
`

@ForbesLindesay
Copy link
Contributor

You cannot get spawn-sync to work in the browser. That is literally impossible. What you can do is use sync-request in the browser, because it provides a separate implementation specifically tailored for the browser.

That grunt file is too large for me to want to get into unpicking and debugging what's going wrong. My advice would be to start with the simplest setup you can possibly have to demonstrate the problem. i.e. use grunt-browserify (with no other grunt modules) to build a script that just contains var sr = require('sync-request');sr('GET', '/'); and see if you can get the resulting script to run without error.

If that still errors, you'll be able to post a really succinct example of what fails, and it's much more likely that someone will have time to help you figure out what's going wrong.

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

No branches or pull requests

4 participants