rollup / plugins

🍣 The one-stop shop for official Rollup plugins
MIT License
3.63k stars 585 forks source link

commonjs - Missing export (Error: ... is not exported by ...) #986

Open danielgindi opened 3 years ago

danielgindi commented 3 years ago

Expected Behavior

Should correctly export whatever is imported.

Actual Behavior

Weird and irrelevant export.

The piece of code (importDeclaration.module.info.code) that fails looks like this:

import * as commonjsHelpers from "commonjsHelpers.js";
import { __exports as mongodb } from "\u0000/Users/user/git/server/node_modules/mongodb/index.js?commonjs-exports"

export default commonjsHelpers.commonjsRequire("/$$rollup_base$$/node_modules/mongodb/index.js", "/$$rollup_base$$/node_modules/mongodb");

@lukastaegert this looks like it's originating from the latest refactor during the circular dependencies support. While mongodb cannot be rolled up normally as it has severe circular dependencies, it seems to not be able to be rolled up dynamically too due to this bug. It looks like this should have been a reexport of __exports or at least *. I tried to look into this and have not found what causes this to compose in such a way. It's probably line 228 in index.js, but how do we make it play well with the other stuff that's being shoved in there?

lukastaegert commented 2 years ago

Seems to be resolved by #1038

phihu commented 2 years ago

There still seems to be the or at least a bug.

In Short: I am importing a lib that depends on axios/axios.

After updating to a version > 18 the import failed with

The requested module ... does not provide an export named 'exports'

as mentioned here and in the related post here: #modernweb-dev/web#1700.

Following the discussion(s) in #1038 and updating to v20.0.4 (beta), I get the following (new) error, though:

Error while transforming ... Could not resolve import "commonjsHelpers.js"

1 | import * as commonjsHelpers from "commonjsHelpers.js";
2 | import require$$0 from...
emensch commented 2 years ago

I'm in the same boat as @phihu - any advice or direction here would be much appreciated :)

phihu commented 2 years ago

Anyone any success? Problem still exists, even using the latest (beta) release v22.0.0-13

mihaisavezi commented 2 years ago

v22.0.0 release and problem still exists

try transforming to commonjs the deepmerge npm module

b-houghton commented 2 years ago

I also am getting the same kind of error message with version 22.0.0 when trying to process @testing-library/dom:

Error while transforming node_modules/@testing-library/dom/dist/helpers.js: Could not resolve import "commonjsHelpers.js".
> 1 | import * as commonjsHelpers from "commonjsHelpers.js";
    |                                  ^
  2 | import { __exports as helpers } from "\u0000/[redacted]/node_modules/@testing-library/dom/dist/helpers.js?commonjs-exports"
  3 |
  4 | "use strict";
songkeys commented 2 years ago

For anyone stuck at this problem: downgrading @rollup/plugin-commonjs to v21 solves for me.

jimblue commented 2 years ago

I'm having the same issue using vitejs when importing photoswipe.

Ssis53 commented 2 years ago

For anyone stuck at this problem: downgrading @rollup/plugin-commonjs to v21 solves for me. It works for me. thx!

mhogara commented 2 years ago

If anyone finds any additional solutions, please update the thread! I'm also facing the same issue and downgrading hasn't helped either.

enjikaka commented 2 years ago

Also having issues with this. Using via web-test-runner. Downgrading to v21 just freezes web-test-runner, while latest release errors with Could not resolve import "commonjsHelpers.js".

jcoglan commented 2 years ago

I'm also having the same problem. v22 gives me the error about commonjsHelpers.js while v21 causes wtr to hang and eventually crash due to running out of memory. This is on a hello-world demo with a single test just to try to get wtr working.

trixter13 commented 2 years ago

Same issue here with web-dev-server. v22 has the mentioned error and v21.1.0 just hangs:

<--- Last few GCs --->

[8057:0x604c890] 384504 ms: Mark-sweep 2008.1 (2082.1) -> 1995.8 (2082.8) MB, 2674.3 / 0.0 ms (average mu = 0.178, current mu = 0.071) task scavenge might not succeed [8057:0x604c890] 386388 ms: Mark-sweep 2009.2 (2082.8) -> 1996.6 (2083.6) MB, 1741.4 / 0.0 ms (average mu = 0.136, current mu = 0.076) task scavenge might not succeed [8057:0x604c890] 388341 ms: Mark-sweep 2010.1 (2083.8) -> 1997.4 (2084.6) MB, 1736.1 / 0.0 ms (average mu = 0.125, current mu = 0.111) task scavenge might not succeed

<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory 1: 0xb09c10 node::Abort() [node] 2: 0xa1c193 node::FatalError(char const, char const) [node] 3: 0xcf8dde v8::Utils::ReportOOMFailure(v8::internal::Isolate, char const, bool) [node] 4: 0xcf9157 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate, char const, bool) [node] 5: 0xeb09f5 [node] 6: 0xeb14d6 [node] 7: 0xebf9fe [node] 8: 0xec0440 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node] 9: 0xf23884 v8::internal::ScavengeJob::Task::RunInternal() [node] 10: 0xdb6f6b non-virtual thunk to v8::internal::CancelableTask::Run() [node] 11: 0xb77a54 node::PerIsolatePlatformData::RunForegroundTask(std::unique_ptr<v8::Task, std::default_delete >) [node] 12: 0xb798b9 node::PerIsolatePlatformData::FlushForegroundTasksInternal() [node] 13: 0x1564956 [node] 14: 0x1577084 [node] 15: 0x15652a8 uv_run [node] 16: 0xa43dd5 node::SpinEventLoop(node::Environment) [node] 17: 0xb4bdb6 node::NodeMainInstance::Run(node::EnvSerializeInfo const) [node] 18: 0xacd592 node::Start(int, char**) [node] 19: 0x7b8494a8fd0a __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6] 20: 0xa4067c [node] Aborted (core dumped)

zipang commented 1 year ago

Same issue here with vitejs : I tried with @rollup/plugin-commonjs@20, @rollup/plugin-commonjs@21, @rollup/plugin-commonjs@latest (23) with the same result..

Error when using sourcemap for reporting an error: Can't resolve original location of error.
'getDefaultExportFromCjs' is not exported by 'commonjsHelpers.js'
'commonjsGlobal' is not exported by 'commonjsHelpers.js'
(...)
'commonjsGlobal' is not exported by 'commonjsHelpers.js'
Error when using sourcemap for reporting an error: Can't resolve original location of error.
'getDefaultExportFromCjs' is not exported by 'commonjsHelpers.js'
'commonjsGlobal' is not exported by 'commonjsHelpers.js'
✓ 3521 modules transformed.
'getAugmentedNamespace' is not exported by commonjsHelpers.js, imported by __vite-browser-external?commonjs-proxy
file: __vite-browser-external?commonjs-proxy:1:75
1: import * as __viteBrowserExternal from "__vite-browser-external"; import { getAugmentedNamespace } from "commonjsHelpers.js"; export default /*@__PURE__*/getAugmentedNamespace(__viteBrowserExternal);
                                                                              ^
error during build:
Error: 'getAugmentedNamespace' is not exported by commonjsHelpers.js, imported by __vite-browser-external?commonjs-proxy
pdmshrestha commented 1 year ago

Any updates on this issue? v24.0.0 and still has issue.

vtron commented 1 year ago

Same issue still with v24.0.1 Could not resolve import "commonjsHelpers.js"

Is this being addressed?

vtron commented 1 year ago

I just tried this setting out and confirmed that it is still happening.

I am not using Vite, but I am am using Web Dev Server https://modern-web.dev/docs/dev-server/overview/ with Storybook, so I am setting strict-requires on the plugin through that config.

The issue seems to be that I am using an ES Module @.***/client-api) which imports a commonjs library.

The imports are for "global/window.js", "core-js/ and "qs".

Everything works fine when I have commonjs plugin active and I don't import the library I am having issues with.

On Fri, Feb 3, 2023 at 12:41 AM Lukas Taegert-Atkinson < @.***> wrote:

Did you try

build: { commonjsOptions: { strictRequires: true } }

in your Vite config?

— Reply to this email directly, view it on GitHub https://github.com/rollup/plugins/issues/986#issuecomment-1415361927, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABFXK7EC6RL5O7CLBY6TLDWVTAC5ANCNFSM5DHI7CGQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

igl commented 1 year ago

I also have this issue using vite --version: vite/4.1.4 darwin-x64 node-v18.14.0 vite dev server works. When i try to build i get RollupError:

"getAugmentedNamespace" is not exported by "commonjsHelpers.js", imported by "/opt/app/node_modules/react-use/esm/index.js?commonjs-proxy".
file: /opt/app/node_modules/react-use/esm/index.js?commonjs-proxy:1:9
1: import { getAugmentedNamespace } from "commonjsHelpers.js"; import * as esm from "/opt/app/node_modules/react-use/esm/index.js"; export default /*@__PURE__*/getAugmentedNamespace(esm);

When i add react-use to commonjsOptions.ignore, the error jumps to the next lib:

"getAugmentedNamespace" is not exported by "commonjsHelpers.js", imported by "/opt/app/node_modules/react-popper/lib/esm/index.js?commonjs-proxy".
file: /opt/app/node_modules/react-popper/lib/esm/index.js?commonjs-proxy:1:9
1: import { getAugmentedNamespace } from "commonjsHelpers.js"; import * as esm from "/opt/app/node_modules/react-popper/lib/esm/index.js"; export default /*@__PURE__*/getAugmentedNamespace(esm);

What am i missing? Any hints on investigating this?

cr4dle commented 11 months ago

I am using Rollup to create an npm pacakge and I have the following versions:

"rollup": "^3.29.4",
"@rollup/plugin-commonjs": "^25.0.7",

I can see in the bundle file that the plugin adds the line import * as commonjsHelpers from 'commonjsHelpers.js';. However, when I install the package on another project I get the above errors. Inspecting what is actually in the npm package, I cannot find the file "commonjsHelpers.js". Could it be a misconfiguration of the plugin?

A simplified rollup.config.ts version:

// omitting imports to save space

const rollupConfiguration = [
    {
        input: ['./src/index.tsx'],
        external: [...Object.keys(pkg.peerDependencies || {}), /@babel\/runtime/],
        output: [
            {
                file: pkg.main,
                format: 'cjs',
                sourcemap: !production,
            },
            {
                file: pkg.module,
                format: 'es',
                exports: 'named',
                sourcemap: !production,
            },
        ],
        plugins: [
            nodeResolve(),
            commonjs({
                sourceMap: !production,             
            }),
            babel({
                exclude: 'node_modules/**',
                presets: ['@babel/preset-react', ['@babel/preset-env']],
                babelrc: false,
                babelHelpers: 'runtime',
                plugins: ['@babel/plugin-transform-runtime'],
            }),
            postcss({
                extract: true,
                modules: false,
                use: ['sass'],
            }),
            typescript({
                sourceMap: !production,
                inlineSources: !production,
                target: 'es6',
            }),
            production && terser(),
        ],
    },
];

export default rollupConfiguration;
soundstep commented 11 months ago

Trying out and adding some babel/rollup plugins:

pnpm dlx create-remix@latest --template remix-run/remix/templates/unstable-vite-express

I also got that kind of error:

"getAugmentedNamespace" is not exported by "commonjsHelpers.js", imported by "remix-vite-experimental/node_modules/.pnpm/core-js@3.33.2/node_modules/core-js/internals/fails.js?commonjs-proxy".

Beginning of stack:

error during build:
RollupError: "getAugmentedNamespace" is not exported by "commonjsHelpers.js", imported by "/Users/RomuQuan/Documents/devserver/workspace/remix-vite-experimental/node_modules/.pnpm/core-js@3.33.2/node_modules/core-js/internals/fails.js?commonjs-proxy".
    at error (remix-vite-experimental/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:2287:30)
    at Module.error (remix-vite-experimental/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:13745:16)
    at Module.traceVariable (remix-vite-experimental/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:14175:29)
    at ModuleScope.findVariable (remix-vite-experimental/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:12615:39)
    at Identifier.bind (remix-vite-experimental/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:8319:40)
import { unstable_vitePlugin as remix } from "@remix-run/dev";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import babel from 'vite-plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';

export default defineConfig({
  plugins: [
    nodeResolve(),
    commonjs(),
    babel(),
    remix(),
    tsconfigPaths()
  ],
});
soundstep commented 11 months ago

@cr4dle I also can't find the file commonjsHelpers.js

kkkkkcnm commented 8 months ago

how to resolve it ?

niyuanfa commented 5 months ago

how to resolve it ?