[V3]TypeError: StateMachine is not a constructor
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.
Can you share the code you used to construct the StateMachine?
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
it is solved,because previously i only include state-machine.js rather than the folder.
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?
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 ?
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.jsis 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. :)
import * as StateMachine from "javascript-state-machine";