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 ./../..
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:
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 ./../..
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 '../'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With