Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not resolve error on .js extension in import with TypeScript #5539

Closed
7 tasks done
ai opened this issue Nov 3, 2021 · 7 comments
Closed
7 tasks done

Could not resolve error on .js extension in import with TypeScript #5539

ai opened this issue Nov 3, 2021 · 7 comments
Labels
duplicate This issue or pull request already exists

Comments

@ai
Copy link

ai commented Nov 3, 2021

Describe the bug

Short

If a TypeScript file has index.js in import, Vite will not find a file.

Long

  1. In native ESM we must have full path to file including index.js. Just import '../dir/' will not work.
  2. In Node.js TS projects it is recommended to have .js extension import '../dir/index.js' even if source have .ts extension because TS is compiler and technically, it will be .js in the end (if I understand TS correctly).
  3. But import '../dir/'import '../dir/index.js' migrate broke Vite with an error:
Could not resolve '../dir/index.js' from index.ts

Changing import '../dir/index.js''import '../dir/ (or to import '../dir/index) will break native ESM in universal Node/browser packages inside my monorepo.

Reproduction

Reproduction

git clone git@github.com:ai/vite-ts-indexjs-bug.git && cd vite-ts-indexjs-bug
yarn
npx vite build

System Info

System:
    OS: Linux 5.14 Fedora 34 (Workstation Edition) 34 (Workstation Edition)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
    Memory: 2.24 GB / 15.33 GB
    Container: Yes
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.0.1 - ~/.asdf/installs/nodejs/17.0.1/bin/node
    Yarn: 1.22.15 - ~/.asdf/shims/yarn
    npm: 8.1.0 - ~/.asdf/plugins/nodejs/shims/npm
  Browsers:
    Chrome: 95.0.4638.69
    Firefox: 94.0

Used Package Manager

yarn

Logs

vite:config bundled config file loaded in 16.49ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-script-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     cssTarget: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] }
  vite:config   },
  vite:config   configFile: '/home/ai/test/vite.config.js',
  vite:config   configFileDependencies: [ 'vite.config.js' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/home/ai/test',
  vite:config   base: '/',
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object] ] },
  vite:config   publicDir: '/home/ai/test/public',
  vite:config   cacheDir: '/home/ai/test/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  vite:config   server: { fs: { strict: undefined, allow: [Array] } },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: {
  vite:config     esbuildOptions: { keepNames: undefined, preserveSymlinks: undefined }
  vite:config   }
  vite:config } +4ms
vite v2.6.13 building for production...
✓ 3 modules transformed.
Could not resolve './lib.js' from index.ts
error during build:
Error: Could not resolve './lib.js' from index.ts
    at error (/home/ai/test/node_modules/rollup/dist/shared/rollup.js:158:30)
    at ModuleLoader.handleResolveId (/home/ai/test/node_modules/rollup/dist/shared/rollup.js:22306:24)
    at /home/ai/test/node_modules/rollup/dist/shared/rollup.js:22300:26
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Validations

@Niputi
Copy link
Contributor

Niputi commented Nov 3, 2021

looks like a duplicate of #3040

@ai
Copy link
Author

ai commented Nov 3, 2021

@rollup/plugin-node-resolve from that issue helped.

Thanks for the search. I tried to find, but was not luck.

@ai ai closed this as completed Nov 3, 2021
@ai ai reopened this Nov 3, 2021
@ai
Copy link
Author

ai commented Nov 3, 2021

Should we improve Vite’s TS docs with the solution for this case?

@Niputi
Copy link
Contributor

Niputi commented Nov 3, 2021

should not be necessary as the issue got fixed with #5510 which will be in the next 2.7 beta

@ai
Copy link
Author

ai commented Nov 3, 2021

Great to see!

@ai ai closed this as completed Nov 3, 2021
@Niputi Niputi added duplicate This issue or pull request already exists and removed pending triage labels Nov 3, 2021
@ai
Copy link
Author

ai commented Nov 3, 2021

Thanks for the very fast response and taking care of tiny DX problems ❤

@Niputi
Copy link
Contributor

Niputi commented Nov 3, 2021

your welcome. thanks for making a good issue description, reproduction etc.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants