javascript-state-machine icon indicating copy to clipboard operation
javascript-state-machine copied to clipboard

[V3]TypeError: StateMachine is not a constructor

Open sunq0001 opened this issue 8 years ago • 7 comments

I downloaded V3, but can't run, showing: TypeError: StateMachine is not a constructor But when i replace the file with V2, it is running properly.

sunq0001 avatar May 25 '17 09:05 sunq0001

Can you share the code you used to construct the StateMachine?

jakesgordon avatar Jun 10 '17 16:06 jakesgordon

Having the same issue.

vendor = node_modules folder.

define([
    'vendor/javascript-state-machine/lib/state-machine'
], function(StateMachine) {

var JavascriptStateMachine = function(config) {
    this.stateMachine = new StateMachine(config);
};

...

TypeError: StateMachine is not a constructor

scotthulluk avatar Jun 27 '17 09:06 scotthulluk

it is solved,because previously i only include state-machine.js rather than the folder.

sunq0001 avatar Jul 14 '17 05:07 sunq0001

I have same problem, I am trying to use "state-machine.js" with TypeScript, after compilation I have:

requirejs.config({ packages: [ { name: "javascript-state-machine", location: "node_modules/javascript-state-machine/dist", main: 'state-machine.js' }, ] }); And then in my module:

define("Game/GameStateMachine", ["require", "exports", "javascript-state-machine"], function (require, exports, StateMachine) { "use strict"; console.log(StateMachine); // Outputs undefined });

Console log outputs "undefined" and than when I try to create instance I have same error that @scotthulluk has: TypeError: StateMachine is not a constructor

@sunq0001 what do You mean by include folder?

pawellen avatar Jul 16 '17 11:07 pawellen

This is also not working for me in require js... Old version worked... define(['lib/state-machine', 'lib/three'], function(StateMachine, THREE) {

//StateMachine is undefined

It appears to have to do with whatever shim, etc. you have in your dist javascript file, makes it incompatible with require.js ?

beiller avatar Jul 26 '17 01:07 beiller

Hi, I have the same problem.

The issue & the solution is here:

In the generated dist/state-machine.js the following code uses a module definition with explicit name at the top of the code:

(function webpackUniversalModuleDefinition(root, factory) {
	if(typeof exports === 'object' && typeof module === 'object')
		module.exports = factory();
	else if(typeof define === 'function' && define.amd)
		define('StateMachine', [], factory);
	else if(typeof exports === 'object')
		exports["StateMachine"] = factory();
	else
		root["StateMachine"] = factory();
})(this, function() {

The problematic line is this one: define('StateMachine', [], factory); Require try to resolve StateMachine as a path, but of course, it is not found. This issue is not reported by require.js <3. On the other hand, this is a path issue, so you can add StateMachine to the require config's path property like this:

'StateMachine': 'url/to/your/state/machine/dist/state-machine'

Maybe this case can be improved the following ways:

  • Include this case to the installation part of the documentation, or create a new doc, where this is described if we want to keep clean the main docs.
  • Explicitly defining an AMD module name for the require.js is a bad practice. See the ocumentation: http://requirejs.org/docs/api.html#modulename . A better solution can be to simply use this snippet: define('StateMachine', [], factory); This code is generated by webpack, so maybe some webpack option can be solve the issue.

If we agree a solution, I'm happy to make the PR. :)

samson84 avatar Dec 08 '17 22:12 samson84

import * as StateMachine  from "javascript-state-machine";

hundan1 avatar Apr 27 '22 07:04 hundan1