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

bug: NexJS build: problem ReferenceError: exports is not defined in ES module scope #40

Open
mendesbarreto opened this issue Mar 1, 2024 · 6 comments · May be fixed by #41
Open

bug: NexJS build: problem ReferenceError: exports is not defined in ES module scope #40

mendesbarreto opened this issue Mar 1, 2024 · 6 comments · May be fixed by #41
Assignees

Comments

@mendesbarreto
Copy link

Description

Hello @jasonsturges, I just updated the package to 2.0.2 and I am receiving this error below,

Collecting page data  ...ReferenceError: exports is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/douglasmendes/Git/xpertsea/web-insights/node_modules/lunarphase-js/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/douglasmendes/Git/xpertsea/web-insights/node_modules/lunarphase-js/dist/index.cjs.js:1:36
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:430:15)

I think this change might be impacting the build for nextJS for production:

https://github.com/jasonsturges/lunarphase-js/commit/38fca43a8a09105bd3d8790a7f895510c421ac7b#diff-7ae45ad102eab3b6d7e7[…]470d7bc6507b6481575d519R19

Renaming the file from index.cjs.js to index.cjs resolved the build issue successfully locally.

@mendesbarreto mendesbarreto changed the title bug: NexJS build problem ReferenceError: exports is not defined in ES module scope bug: NexJS build: problem ReferenceError: exports is not defined in ES module scope Mar 1, 2024
@jasonsturges
Copy link
Owner

Strangely I don't receive this in Next.js

image

@jasonsturges jasonsturges self-assigned this Mar 1, 2024
@jasonsturges
Copy link
Owner

jasonsturges commented Mar 1, 2024

Both TypeScript and JavaScript are functional in Next.js, with different targets.

Even before when I used Rollup directly, CommonJS has always been distributed as .js
image

Can you give some insights to your Next.js config, and TSConfig if you're using TypeScript?

Are you using this as import or require?

jasonsturges added a commit that referenced this issue Mar 1, 2024
@jasonsturges
Copy link
Owner

@mendesbarreto Try version 2.0.3 - this removes the module type specifier.

Perhaps I'm missing something here, but I'd like to understand what's occurring in your environment. If you could share some details would help to trace this.

@mendesbarreto
Copy link
Author

mendesbarreto commented Mar 4, 2024

@jasonsturges It's working now, thank you so much for your help.

Now your questions:

Are you using this as import or require?

We are using import like:

import { Moon } from 'lunarphase-js';

It's quite odd because I've never encountered this issue with your library before. It only started happening after this recent change,

The command I am running is:

 NODE_ENV=production next build

Let me know any specific details about our project here you want to know.

@jasonsturges
Copy link
Owner

@mendesbarreto Thanks for the information.

Some issue interpreting ES Modules vs Common JS... I'll continue to trace it, and hopefully will match a similar environment with the issue.

@jasonsturges
Copy link
Owner

Ah, finally able to replicate this in a Node.js environment - I suppose that makes sense for Next.js

index.cjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.

...although strangely I don't experience this issue in Next.js

This defeats the purpose of configuration, though:

  "main": "dist/index.cjs.js",
  "module": "dist/index.es.js",

Need to research how other packages are handling this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants