Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use import in nodejs will cause error #1449

Closed
zhmushan opened this issue May 28, 2018 · 7 comments
Closed

Use import in nodejs will cause error #1449

zhmushan opened this issue May 28, 2018 · 7 comments

Comments

@zhmushan
Copy link

❔ Question

Use import in nodejs will cause error.

πŸ”¦ Context

When I use parcel to package a node application written in typescript, I can only use require to reference third-party dependencies.

πŸ’» Code Sample

import * as koa from 'koa'
const app = new koa() // error
const koa = require('koa')
const app = new koa() // success

🌍 Your Environment

Software Version(s)
Parcel 1.8.1
Node 8.11.1
Yarn 1.6.0
Operating System windows 10
@DeMoorJasper
Copy link
Member

Perhaps this helps #202

@zhmushan
Copy link
Author

zhmushan commented May 28, 2018

Thank you.
But I don't think this is an error caused by tsconfig. When I use import * as koa from 'koa', parcel changes the variable koa to {default: Application}. The correct one should be {Application}.
At the same time, my tsconfig file did not make any configuration for the path

@DeMoorJasper
Copy link
Member

DeMoorJasper commented May 28, 2018

you could use this as a workaround.

import {default as koa} from 'koa'

@fathyb Any idea why this doesn't work?

@zhmushan
Copy link
Author

What I mean is that koa has no default export, but parcel thinks it has a default export, and set newObj.default = obj in output file

@mohsen1
Copy link
Contributor

mohsen1 commented May 28, 2018

Related to #1378

Parcel needs to understand allowSyntheticDefaultImports config of TypeScript.

@zhmushan
Copy link
Author

@mohsen1 you are right, thanks

@fathyb
Copy link
Contributor

fathyb commented May 28, 2018

Solution (replace express with koa, allowSyntheticDefaultImports has nothing to do with the runtime) :

For a definitive fix set the compiler option esModuleInterop to true and use import express instead of import * as express.

The problem :

The ES6 specification defines the notion of "namespace object". A namespace object is namespaceObject in this statement : import * as namespaceObject from 'a-module'. The typeof this object is object, you are not supposed to be able to call it.

You were using import * as express until now because express is a CommonJS module, for Node.js, it is exported using module.exports. However it is illegal in the ES6 spec, and TypeScript now warns you.

The solution :

Setting esModuleInterop to true will make TypeScript wrap your import calls to check if the module is an ES6 module or a CommonJS. If it's a CommonJS module and you are using import default from 'module' TypeScript will find out and return the correct CommonJS module.

From the TypeScript release note :

Note: The new behavior is added under a flag to avoid unwarranted
breaks to existing code bases. We highly recommend applying it both to
new and existing projects. For existing projects, namespace imports
(import * as express from "express"; express();) will need to be
converted to default imports (import express from "express";
express();).

@fathyb fathyb closed this as completed May 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants