#Importing typescript-only packages.

29 messages · Page 1 of 1 (latest)

heavy haven
#

I would like to import typescript-only npm package.
When building i receive "Cannot use import statement outside a module" error.
How can I make sure that this typescript file gets transpiled or do i have to include build step in this package that emits .js and .d.ts files?

mint rain
#

there is no such thing as typescript-only packages

#

the errors you are facing is related to ES modules

#

you can only use an import statement in a file that is an ES module

#

a file is only an ES module if it has at least one export statement

#

👉 to solve the problem, just add an export statement to your file, to turn it into an ES module

#

if you have nothing to export, you can export an empty object like so

export {};
#

👉 you could also set the option moduleDetection to force to force all files to be considered as ES modules

#

@heavy haven

lime current
heavy haven
#

The problem is that when this code is in node_modules, it is not treated like regular .ts file

#

And it seems to be related to the bundling process

#

So I would like to know if this is somehow doable with tsc alone or other tools

#

Normally tsc, will generate file with require/import statements so when the output will run it will have problem because node cannot run ts files

#

So, the general question is can node_module not include build step to js, so that the file gets imported in project just like it was an ordinary file in project.

heavy haven
#

Also I want to say that I would not like to set whole project's where package is imported package.json to include type: "module"

lime current
#

build it to .js with declaration: true and declarationMap: true

lime current
#

I think you would have to do a folder with symlinks into the project folder if you wanted that

#

@heavy haven

heavy haven
lime current
#

Oh really?

#

So your issue is that tsc doesn't compile the .ts files in node_modules? I wouldn't expect it to

#

Have you specified the folder as included in tsconfig "include" section?

#

Hmm that would gives problems because you'd get a node_modules folder in dist, or it would complain that its not in dist