jorgebucaran / colorette

🌈Easily set your terminal text color & styles
MIT License
1.61k stars 51 forks source link

require() of ES modules is not supported #65

Closed its-dibo closed 3 years ago

its-dibo commented 3 years ago

I have an issue when I try to run webpack with webpack.config.ts written in typescript and ts-node has "require": ["tsconfig-paths/register"]

// tsconfig.json

{
  compilerOptions:{
    paths: { /* ----------------- */ }
  },
 'ts-node':{
   "require": ["tsconfig-paths/register"],
   "compilerOptions": {
      "sourceMap": false,
      "module": "commonjs"
    }
}
}

when I remove "require": ["tsconfig-paths/register"] from ts-node it runs, but webpack cannot detect aliases.

I already set module: commonjs in my tsconfig.json and removed type: module from my package.json

trace


> webpack

(node:5932) Warning: require() of ES modules is not supported.
require() of /home/node_modules/colorette/index.js from /home/node_modules/webpack-cli/lib/utils/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /home/node_modules/colorette/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/node_modules/colorette/package.json.
(node:5932) UnhandledPromiseRejectionWarning: /home/node_modules/colorette/index.js:28
export const options = Object.defineProperty({}, "enabled", {
^^^^^^

SyntaxError: Unexpected token 'export'
    at new Script (vm.js:84:7)
    at NativeCompileCache._moduleCompile (/home/node_modules/v8-compile-cache/v8-compile-cache.js:240:18)
    at Module._compile (/home/node_modules/v8-compile-cache/v8-compile-cache.js:184:36)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (/home/node_modules/v8-compile-cache/v8-compile-cache.js:159:20)
    at Object.get colors [as colors] (/home/node_modules/webpack-cli/lib/utils/index.js:3:16)
    at Object.error (/home/node_modules/webpack-cli/lib/utils/logger.js:5:58)
(node:5932) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:5932) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
`
jorgebucaran commented 3 years ago

Colorette should work out of the box on old Node because we define entry points for it here.

its-dibo commented 3 years ago

ok, so how to solve this issue? I use node v12.14.1

as mentioned in the error trace, your package defined type: module, here so it treats index.js as ES module.

jorgebucaran commented 3 years ago

If you are importing an ES module in Node v12.* you also need to specify the --experimental-modules flag when running your program. Did you try that?

its-dibo commented 3 years ago

I use webpack which uses ts-node which registers tsconfig-paths which on its turn uses colorette.

I set module: commonjs to my tsconfig.json and can't require to use ES modules.

I don't run node by myself at all

jorgebucaran commented 3 years ago

requireing Colorette should work out of the box when using CommonJS. This looks like a webpack thing, so closing.