inokawa / remark-docx

remark plugin to compile markdown to docx (Microsoft Word, Office Open XML).
https://inokawa.github.io/remark-docx/
MIT License
37 stars 13 forks source link

import issues #36

Closed hgodinho closed 1 year ago

hgodinho commented 1 year ago

Describe the bug When I made the PR I tested using the storybook app, now Im trying to use in another application and getting errors: if I try the default import:

import docx from "remark-docx";

// error
C:\Users\55119\Elucidário.art\elucidario\node_modules\.pnpm\remark-docx@0.1.2\node_modules\remark-docx\lib\index.js:4
var unistUtilVisit = require('unist-util-visit');       
                     ^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\55119\Elucidário.art\elucidario\node_modules\.pnpm\unist-util-visit@4.1.2\node_modules\unist-util-visit\index.js from C:\Users\55119\Elucidário.art\elucidario\node_modules\.pnpm\remark-docx@0.1.2\node_modules\remark-docx\lib\index.js not supported.
Instead change the require of C:\Users\55119\Elucidário.art\elucidario\node_modules\.pnpm\unist-util-visit@4.1.2\node_modules\unist-util-visit\index.js in C:\Users\55119\Elucidário.art\elucidario\node_modules\.pnpm\remark-docx@0.1.2\node_modules\remark-docx\lib\index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (C:\Users\55119\Elucidário.art\elucidario\node_modules\.pnpm\remark-docx@0.1.2\node_modules\remark-docx\lib\index.js:4:22)
    at async Promise.all (index 0) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.12.1
 ELIFECYCLE  Command failed with exit code 1.

if I try to import the mjs module:

import docx from "remark-docx/lib/index.mjs";

// error
file:///C:/Users/55119/Elucid%C3%A1rio.art/elucidario/node_modules/.pnpm/remark-docx@0.1.2/node_modules/remark-docx/lib/index.mjs:4
import { convertInchesToTwip, Packer } from 'docx';     
                              ^^^^^^
SyntaxError: Named export 'Packer' not found. The requested module 'docx' 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 'docx';
const { convertInchesToTwip, Packer } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:123:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:189:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Node.js v18.12.1
 ELIFECYCLE  Command failed with exit code 1.
inokawa commented 1 year ago

It seems that default import of remark-docx is treated as commonjs but unist-util-visit is published as esm only. So it may be solved if you transpile unist-util-visit to commonjs, such as transpilePackages in Next.js.

I'm not sure why remark-docx is detected as commonjs. It depends on your build tools.

hgodinho commented 1 year ago

Actually I'm not using any building script. I created this repo with a minimal test, just as documented in README.md and the error still occurs: https://github.com/hgodinho/remark-docx-test

inokawa commented 1 year ago

Published in 0.1.4.