commonify icon indicating copy to clipboard operation
commonify copied to clipboard

Can't be imported from CommonJS and/or instructions not clear?

Open JoshuaCWebDeveloper opened this issue 3 years ago • 1 comments

This is a really neat idea! However, my first attempt at forking a package and republishing it (by following the instructions in the README.md) resulted in the following error when I tried to run my CJS project using the package I just forked:

/node_modules/@vallerance/globby/index.js:1
import fs from 'node:fs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.@vallerance/globby (/home/joshua/WebDev/projects/no-code/dist/packages/tsc-compilers/main.js:606:18)
    at __webpack_require__ (/home/joshua/WebDev/projects/no-code/dist/packages/tsc-compilers/main.js:672:41)

This was because index.js had ESM style code.

I figured out I could solve this by doing the following for each forked package:

cd package/
cp ../transpiled ./

This replaced the ESM style code with CJS style code. From there, I could simply increment the version and republish. This resolved the error I was getting from my project and it ran fine with the forked packages.

I'm not sure if this is a bug in this project somewhere, or if maybe the instructions weren't clear (or maybe missing a step).

JoshuaCWebDeveloper avatar May 01 '22 05:05 JoshuaCWebDeveloper

Opps! I'm sure it's a bug here in commonify due to the node: prefix. As said in the readme this mostly an experiement and I didn't test it extensively on a lot of packages. I don't plan on working much on it, but if you find a solution I'm happy to merge a PR!

mifi avatar May 05 '22 21:05 mifi