skip to Main Content

Here are 3 files:

// ./app/index.ts
const app = {
  test: 1,
  ...
}
export default app
// ./app.ts
import app from './app/index'

export default app
// ./index.vue
import { test } from './app.ts'

console.log(test)

When I run index.vue an error shows:

SyntaxError: The requested module ‘/@fs/Users/VJ/Desktop/project/app.ts?t=1691652606598’ does not provide an export named ‘test’

How can I fix it?

2

Answers


  1. here is the way that should work.

    ./app/index.ts:

    const app = {
        test: 1,
        // ...
    };
    
    export { app };
    

    ./app.ts:

    import { app } from './app/index';
    
    export default app;
    

    ./index.vue:

    import app from './app.ts';
    
    console.log(app.test);
    
    Login or Signup to reply.
  2. For import { test } from './app.ts' to work app.ts needs to have a named export test. Yours only has a default export.

    If you don’t want to explicitly export every property of the object I suggest to use object destructuring in index.vue:

    import app from './app.ts'
    const {test} = app
    
    console.log(test)
    

    FWIW, if you have to use app.ts as intermediate module (for whatever reason) you can rewrite it to

    export {default} from './app/index'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search