Skip to content

[@rollup/plugin-node-resolve] Not adding .js suffix while importing from an external library's subpath without exports field #1856

@satouriko

Description

@satouriko
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';
import typescript from '@rollup/plugin-typescript';
import del from 'rollup-plugin-delete';

export default {
  input: './src/index.ts',
  output: {
    dir: './lib',
    preserveModules: true,
    preserveModulesRoot: 'src',
    sourcemap: true,
  },
  external: [/node_modules/],
  plugins: [
    commonjs(),
    resolve({ preferBuiltins: true }),
    json(),
    typescript(),
    del({
      targets: 'lib/*',
      runOnce: true,
    }),
  ],
};

Given an import like, with node_modules set external,

import stringify from 'qs/lib/stringify';

Expected Behavior

Since this is an esm output, and the library qs doesn't have an exports field in its package.json. To mimic and comply with node's behavior, I believe an expected output should be

import stringify from 'qs/lib/stringify.js';

Actual Behavior

it produces, which would fail on node execution

import stringify from 'qs/lib/stringify';

Additional Information

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions