I dynamically create a function in the instance Foo1 named test . I create this function using eval . I would expect that this feature will have access to the class Foo2 , but I get ReferenceError: Foo2 is not defined .
I opened with the problem of Babel about it can be found here
If you want to run the sample code yourself, download it from here and follow the instructions in README.MD to play.
For start:
npm install
npm run start
naviage to localhost: 8080
Here is the directory structure for my minimum, complete and verifiable examples in my environment:
root - src - Foo1.js - Foo2.js - .babelrc - app.js - package.json
Foo1.js
import Foo2 from './Foo2.js'; export default class Foo1 { constructor() { {" + text + "})"); import Foo2 from './Foo2.js'; export default class Foo1 { constructor() { new Foo2 (); foo2.test ();'); import Foo2 from './Foo2.js'; export default class Foo1 { constructor() {
Foo2.js
export default class Foo2 { test() { console.log('i\'m working!'); } } '!); export default class Foo2 { test() { console.log('i\'m working!'); } }
.babelrc
{ "presets": ["es2015"] } ] { "presets": ["es2015"] }
app.js
import express from 'express'; import http from 'http'; import Foo1 from './src/Foo1.js'; const app = express(); const server = http.createServer(app); app.get('/', (req, res) => { let test = new Foo1(); test.test(); res.end('bye'); }); server.listen(8080); , res) => { import express from 'express'; import http from 'http'; import Foo1 from './src/Foo1.js'; const app = express(); const server = http.createServer(app); app.get('/', (req, res) => { let test = new Foo1(); test.test(); res.end('bye'); }); server.listen(8080);
package.json
{ "name": "test", "scripts": { "start": "./node_modules/babel-cli/bin/babel-node.js ./app.js" }, "dependencies": { "http": "*", "express": "*", "babel-cli": "^6.7.7", "babel-core": "^6.7.7", "babel-polyfill": "^6.3.14", "babel-preset-es2015": "^6.6.0" } } 6.7.7", { "name": "test", "scripts": { "start": "./node_modules/babel-cli/bin/babel-node.js ./app.js" }, "dependencies": { "http": "*", "express": "*", "babel-cli": "^6.7.7", "babel-core": "^6.7.7", "babel-polyfill": "^6.3.14", "babel-preset-es2015": "^6.6.0" } } 6.7.7", { "name": "test", "scripts": { "start": "./node_modules/babel-cli/bin/babel-node.js ./app.js" }, "dependencies": { "http": "*", "express": "*", "babel-cli": "^6.7.7", "babel-core": "^6.7.7", "babel-polyfill": "^6.3.14", "babel-preset-es2015": "^6.6.0" } } 6.3.14", { "name": "test", "scripts": { "start": "./node_modules/babel-cli/bin/babel-node.js ./app.js" }, "dependencies": { "http": "*", "express": "*", "babel-cli": "^6.7.7", "babel-core": "^6.7.7", "babel-polyfill": "^6.3.14", "babel-preset-es2015": "^6.6.0" } } "^ 6.6.0" { "name": "test", "scripts": { "start": "./node_modules/babel-cli/bin/babel-node.js ./app.js" }, "dependencies": { "http": "*", "express": "*", "babel-cli": "^6.7.7", "babel-core": "^6.7.7", "babel-polyfill": "^6.3.14", "babel-preset-es2015": "^6.6.0" } }
Now, if I changed the class Foo2.js to the previous version of javascript, it works like a charm:
function Foo2() { } Foo2.prototype.test = function() { console.log('i\'m working!'); }; module.exports = Foo2; '!); function Foo2() { } Foo2.prototype.test = function() { console.log('i\'m working!'); }; module.exports = Foo2;