dsherret/code-block-writer

TypeError: CodeBlockWriter is not a constructor

Saopetrik opened this issue · 2 comments

Hey there,

I have installed the library using the command npm install --save code-block-writer

I have created a file main.js then added the example mentioned in readme.

main.js file


const CodeBlockWriter = require("code-block-writer");

const writer = new CodeBlockWriter({
    // optional options
    newLine: "\r\n",         // default: "\n"
    indentNumberOfSpaces: 2, // default: 4
    useTabs: false,          // default: false
    useSingleQuote: true     // default: false
});

writer.write("class MyClass extends OtherClass").block(() => {
    writer.writeLine(`@MyDecorator(1, 2)`);
    writer.write(`myMethod(myParam: any)`).block(() => {
        writer.write("return this.post(").quote("myArgument").write(");");
    });
});

console.log(writer.toString());

then to run it I did node main.js

I am getting an error :


const writer = new CodeBlockWriter({
               ^

TypeError: CodeBlockWriter is not a constructor
    at Object.<anonymous> (A:\practise\main.js:3:16)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.12.1

Sorry I never used JS/TS . So can you please tell me how to use it correctly.

Since you're using require and not typescript with esModuleInterop, you'll need to do:

const CodeBlockWriter = require("code-block-writer").default;

Yes..Thank you.It solved the problem