lydell / js-tokens

Tiny JavaScript tokenizer.
MIT License
503 stars 31 forks source link

TypeError: (0 , js_tokens_1.default) is not a function #34

Closed pavlyutkin closed 1 year ago

pavlyutkin commented 1 year ago

console.log(Array.from(jsTokens('Привет, а?'))); ^ TypeError: (0 , js_tokens_1.default) is not a function

pavlyutkin commented 1 year ago

Use import jsTokens from 'js-tokens';

lydell commented 1 year ago

Hi! Can you provide steps to reproduce?

For example:

❯ node -v
v18.9.0

❯ npm i js-tokens

added 1 package, and audited 2 packages in 555ms

found 0 vulnerabilities

❯ cat test.mjs
import jsTokens from 'js-tokens';
console.log(jsTokens);

❯ node test.mjs
[GeneratorFunction: jsTokens]
pavlyutkin commented 1 year ago
PS C:\dev\self\wordz-backend> node -v
v19.2.0
PS C:\dev\self\wordz-backend> yarn add js-tokens
yarn add v1.22.4
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@2.3.2: The platform "win32" is incompatible with this module.
info "fsevents@2.3.2" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
warning " > @nestjs/swagger@6.1.4" has incorrect peer dependency "@nestjs/common@^9.0.0".
warning " > @nestjs/swagger@6.1.4" has incorrect peer dependency "@nestjs/core@^9.0.0".
warning " > ts-loader@9.4.2" has unmet peer dependency "webpack@^5.0.0".
[4/4] Building fresh packages...

success Saved 1 new dependency.
info Direct dependencies
└─ js-tokens@8.0.0
info All dependencies
└─ js-tokens@8.0.0
Done in 9.96s.
lydell commented 1 year ago

Did you forget to paste some parts? I only see node -v and yarn add js-tokens.

pavlyutkin commented 1 year ago
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { json, urlencoded } from 'express';
import * as morgan from 'morgan';
import jsTokens from 'js-tokens';

console.log(Array.from(jsTokens('Привет, а?')));

Console:

C:\dev\self\wordz-backend\src\main.ts:8
console.log(Array.from(jsTokens('Привет, а?')));
                               ^
TypeError: (0 , js_tokens_1.default) is not a function
    at Object.<anonymous> (C:\dev\self\wordz-backend\src\main.ts:8:32)
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Function.Module._load (node:internal/modules/cjs/loader:922:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:82:12)
    at node:internal/main/run_main_module:23:47
lydell commented 1 year ago
❯ node -v
v19.2.0

❯ yarn add js-tokens
yarn add v1.22.19
warning ../../package.json: No license field
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...

success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ js-tokens@8.0.0
info All dependencies
└─ js-tokens@8.0.0
✨  Done in 0.32s.

❯ cat test.mjs
// import { NestFactory } from '@nestjs/core';
// import { AppModule } from './app.module';
// import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
// import { json, urlencoded } from 'express';
// import * as morgan from 'morgan';
import jsTokens from 'js-tokens';

console.log(Array.from(jsTokens('Привет, а?')));

❯ node test.mjs
[
  { type: 'IdentifierName', value: 'Привет' },
  { type: 'Punctuator', value: ',' },
  { type: 'WhiteSpace', value: ' ' },
  { type: 'IdentifierName', value: 'а' },
  { type: 'Punctuator', value: '?' }
]
lydell commented 1 year ago

What you posted mentions main.ts – TypeScript. You are clearly skipping steps. Please stop wasting my time and create a minimal reproduction.

pavlyutkin commented 1 year ago

Sorry, the problem was solved by adding "esModuleInterop": true in tsconfig.json https://www.typescriptlang.org/tsconfig#esModuleInterop