question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Blocking bug with @pixi/node, unable to use with non-CommonJS modules

See original GitHub issue

I’ve been experimenting with the @pixi/node package available in v6.5.0-rc.2. I have no problems when it’s using CommonJS (e.g., require('@pixi/node')), but I am having issues using modules (e.g., import * as PIXI from '@pixi/node').

Here’s a simple example:

https://github.com/bigtimebuddy/pixi-node-example/blob/module/index.mjs

Running this produces the following error:

import { Extract } from '@pixi/extract';
         ^^^^^^^
SyntaxError: Named export 'Extract' not found. The requested module '@pixi/extract' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@pixi/extract';
const { Extract } = pkg;

    at ModuleJob._instantiate (internal/modules/esm/module_job.js:124:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:179:5)
    at async Loader.import (internal/modules/esm/loader.js:178:24)
    at async Object.loadESM (internal/process/esm_loader.js:68:5)
    at async handleMainPromise (internal/modules/run_main.js:59:12)

Having all packages be both CommonJS and ESM seems to confuse Node, even with the proper type. I have not found a way to force dependencies to use modules. Kinda stuck, any ideas?

A workaround would be to bundle everything (except the 3rd party dependencies like canvas, gl, etc) so that it’s a single file. Providing cjs and mjs files seems like it would work.


EDIT: think I figured out what needs to happen here:

TODO

  • Add exports to assets (#8504)
  • Bundle ismobilejs or add to adapter (#8506)
  • Rename esm extension to “.mjs” for all packages (#8505)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
UltraCakeBakerycommented, Jul 18, 2022

I came across https://antfu.me/posts/publish-esm-and-cjs a while ago when I was running into this issue with a side project I’m working on. The configurations used in the package.json might be helpful here

0reactions
Julien-Marcoucommented, Jul 17, 2022

NodeJS requires a .mjs extension or "type": "module" inside the package.json in order to import a file as an ES Module.

I don’t know if it’s possible to have several package.json, one per distribution.

EDIT:

Looks like it’s possible: https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html

For each packages, adding a dist/esm/package.json, that contains:

{
  "type": "module"
}

And a dist/cjs/package.json that contains:

{
  "type": "commonjs"
}

Seems to be a solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node Modules at War: Why CommonJS and ES ... - Code Red
Since the dawn of Node, Node modules were written as CommonJS modules. We use require() to import them. When implementing a module for...
Read more >
RequireJS API
The RequireJS syntax for modules allows them to be loaded as fast as possible, even out of order, but evaluated in the correct...
Read more >
Unable to import ESM .ts module in node - Stack Overflow
It seems you are looking to use ESM with Node and TS. ... it will enable you to have imports with no extensions...
Read more >
Modules: Packages | Node.js v19.3.0 Documentation
It can be used to load JavaScript CommonJS modules. ... startup contains no package.json file, // and therefore inherits the "type" value from...
Read more >
rollup.js
For Node.js: # compile to a CommonJS module ('cjs') rollup main.js --file bundle.js --format cjs ... Rollup should typically be used from the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found