ts-node tries to compile any imported file as Typescript (and fails when it's not)

Created on 17 Aug 2016  路  5Comments  路  Source: TypeStrong/ts-node

I am trying to setup some tests with ts-node and tape for my React components, but I am encountering some imports like import 'style.css'; which are meant to be processed by Webpack and ignored by Typescript. The problem is that ts-node attempts to parse it as Typescript and then (obviously) fails.

Is there something we can do here so solve this?

invalid question

Most helpful comment

@onlywei That's out of scope for this module though, I'm sure there's projects well defined for this sort of things (it's really just doing require.extensions['.css'] = () => undefined).

All 5 comments

This is not ts-node, this is just how node works. There isn't any way to do what you want in node except to use conditional requires.

Okay, thanks for the tip. I suppose I should use something like this then?

@JabX That looks like it could work. By default, any require will be execute by node and that's exactly (educated guess) what's happening here. You'd be running into a runtime error, not because of TypeScript, but because node is trying to evaluate your CSS and JavaScript.

I apologize for resurrecting this thread more than two years after its closing, but CSS file imports remain the primary reason why I have to resort to using Webpack on the server-side instead of being able to use ts-node.

My understanding is that a statement like import 'style.css'; will be converted by TypeScript to require('style.css');. I am hoping for a way in my tsconfig.json to specify that import 'style.css'; should be converted into a blank empty line instead.

@onlywei That's out of scope for this module though, I'm sure there's projects well defined for this sort of things (it's really just doing require.extensions['.css'] = () => undefined).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OliverJAsh picture OliverJAsh  路  3Comments

watzon picture watzon  路  3Comments

mattdell picture mattdell  路  4Comments

aj-r picture aj-r  路  3Comments

sodiumjoe picture sodiumjoe  路  4Comments