7

I have the following folder structure

src/
  index.js
  lib/
    test.js
dist/
examples/
  example.js

src/lib/test.js

export default class Test {}..

src/index.js

import App from './lib/test.js'
export default App

examples/example.js

import {App} from './../..'

=> App is undefined

How can I set my index.js as entrypoint and export my app there?

edit: I'm using babel-node as transpiler and start it with

nodemon test.js --exec babel-node --presets es2015,stage-2 --watch ./../..
4
  • 1
    Shouldn’t it be from '../src' instead of from './../..'? Commented Jul 18, 2017 at 19:17
  • And you’ve shown us export class Test, but not a default export. I don’t think export {default as App} would be valid to do that anyway…? Commented Jul 18, 2017 at 19:21
  • 1
    You're exporting default but importing named export App - which doesn't exist. Commented Jul 18, 2017 at 19:44
  • @estus thanks now it's not undefined anymore! Commented Jul 18, 2017 at 19:46

2 Answers 2

3

The import and export is not natively supported by Node.

You need to use a transpiler like Babel if you want to use that syntax.

The Node way is to use module.exports and require().

See this for more info:

Update

Here:

export {default as App} from './src/lib/test.js'

you're not exporting "from" - you import from.

Maybe you meant:

import App from './src/lib/test.js';

and then you can export that in turn.

With normal Node syntax it would be:

src/lib/test.js

class Test {
  // ...
}
module.exports = { Test };

src/index.js

const { Test: App } = require('./lib/test.js');

examples/example.js

const { App } = require('../src');

Also note that according to your directory structure your paths are incorrect: it should be ./lib/test.js instead of ./src/lib/test.js and ../src instead of ./../..

Sign up to request clarification or add additional context in comments.

5 Comments

But then the OP would get a syntax error, not undefined, no?
I'm using babel-node as transpiler to start it with "nodemon test.js --exec babel-node --presets es2015,stage-2 --watch ./../.."
@coffeecup It would be much easier to answer your questions if you included the relevant info in the question in the first place.
I changed export class to "export default" in my test.js file, and changed the export from.. to import test from in my index.js, now when I console.log(test) I can see [Function: App]. How would I export this properly to be able to import it in my example.js?
my index.js is import Test from ..... and export default Test, but in my example.js it's still undefined
1

I would just put src/index.js as main in the package.json and just run nodemon without the watch param.

By default nodemon monitors the current working directory. If you want to take control of that option, use the --watch option to add specific paths

Also paths sould look like this:

src/lib/test.js

export default class Test {}

src/index.js

export {default as App} from './lib/test.js'

examples/example.js

import {App} from '../'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.