#How to use "import * as" with swc?

13 messages · Page 1 of 1 (latest)

tame bronze
#

I have set up a new NestJS server via CLI. Then I installed the compression package via npm i --save compression.

My main.ts looks like this:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import * as compression from 'compression';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(compression());
  await app.listen(3000);
}
bootstrap();

Compare https://docs.nestjs.com/techniques/compression

Now when I start the project via nest start -b swc I get the following error message:

/test/src/main.ts:7
  app.use(compression());
          ^
TypeError: _compression is not a function
    at bootstrap (/test/src/main.ts:7:11)

How can I configure swc to allow the use of import * as ...?

ruby knot
#

You dont need --save
But everything seems to check out

#

Setup wise at least
Dont know much about swc tho
Will have to look at it

ruby knot
#

might be a chance swc does not support es6
core team/mods should know this (but please dont start pinging them)
doubt that is the case tho

#

does it work when you use the old require()

tame bronze
#

Hello @ruby knot , thank you very much for your hints. With require it works. But I hope for another solution, because we want to use swc in a bigger existing project and we don't really want to change everything there to require.

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
const compression = require('compression');

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(compression());
  await app.listen(3000);
}
bootstrap();
ruby knot
# tame bronze Hello <@211445675701829632> , thank you very much for your hints. With `require`...

you dont have to change everything to require
nodejs is fine with a require and and the rest being import
i understand you would prefer to import them to keep code consistent

but there are some other things you can try to solve it
sometimes it helps to restart i had this a few times

second option is adding this to the package.json and see if that helps
first you can try adding this to your package.json

{
  "type": "module"
}

third thing is to re-install the packages by deleting the node_modules folder and running npm i

the fourth thing is kinda cursed and something i prefer not to use unless i encounter similar things like yours

import compression = require('compression')

sometimes a import does not want to work and then i use this
and some later day i change it back to a regular import and it magically works, i do not know why this happens either
JS is strange

beleave me i have seen your problem enough times 😅

tame bronze
#

@ruby knot Thanks for your support! I have now all the places that were previously

import * as xxx = required("yyy");
// or
const xxx = required("yyy");

changed into:

import xxx = required("yyy");

This worked.

real sapphire
#

@tame bronze how your tsconfig file looks like?

tame bronze
#

@real sapphire

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowJs": true,
    "target": "es2020",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "noImplicitOverride": true
  },
  "exclude": ["node_modules", "dist"]
}
real sapphire
tame bronze
#

Unfortunately this did not work for all packages. I got error messages when I tried to import the package rfdc like this: import * as rfdc from 'rfdc';