Ts-node: Ts-node fails when using symlinks

Created on 21 Jul 2017  Â·  9Comments  Â·  Source: TypeStrong/ts-node

Hi,

I'm reporting here an issue I described here https://github.com/TypeStrong/ts-node/issues/391#issuecomment-316428908.
To reproduce the problem you can just use the frameworks we are developing.

git clone -b housekeeping [email protected]:tierratelematics/prettygoat.git
git clone -b 4.0.0-alignment [email protected]:tierratelematics/prettygoat-cassandra-store.git

You can download the sources from there and install deps on both projects. Then go to prettygoat-cassandra-store and npm link prettygoat. You can see that both tsc and ts-node are failing. Link also rxjs (the one in the prettygoat's node_modules) to the cassandra module and tsc now it's working.

The commands I'm using to test tsc/ts-node:
tsc --outDir build
ts-node scripts/prettygoat-cassandra-store.ts

bug help wanted

Most helpful comment

@kirillgroshkov I know is late, but setting environment var NODE_PRESERVE_SYMLINKS to 1 should work (only node 7.1 or newer).

All 9 comments

I've similar issue when using a bin file, it may be related.

The bin file:

#!/usr/bin/env node
"use strict";
const config = require("../tsconfig.json");
require("ts-node").register(config);
require("../cli/index");

So this is a bin file, a symlink to that file is created in /usr/bin/BIN which is pointing to a real path, eg. /home/tlaziuk/project/bin/index.js.

../cli/index is a TS file which compiles without any problems when using the tsc.

When executing the bin inside the project root everything works fine, but when I want to execute that anywhere else it looks like there are no @types installed - errors like Cannot find module 'path'. are thrown during the TS compile.

> ts-node "--version"

ts-node v3.3.0
node v8.2.1
typescript v2.4.2

Same problem.

Problem
File structure:

|- __shared
    |- ShareClass.ts
|- backend
    |- <shared>  // symlink to ../__shared
    |- index.ts

__shared/ShareClass.ts

import XXX from 'some-node-modules';
...

backend/index.ts

import XXX from './shared/ShareClass';
ts-node index.ts

It will throw error:

TSError: ⨯ Unable to compile TypeScript
..__shared\ShareObj.ts (1,26): Cannot find module 'tsrpc-protocol'. (2307)

Expect
shared/ShareObj.ts is treated as its symlink path.

Demo
A demo project here

cd backend
npm install
ts-node index.ts

Hi guys, I just find a solution to resolve this.
Just use:

ts-node --preserve-symlinks index.ts

@blakeembrey The problem is mainly because in TypeScript preserveSymlinks is true by default, while it is false in NodeJS by default.

@blakeembrey Maybe it's better to auto set --preserve-symlinks if it is not false in tsconfig.json ?

--preserve-symlinks doesn't work:( Also, it's not documented anywhere. Is it still supported? Any workarounds?

node -r ts-node/register --preserve-symlinks script.ts

@kirillgroshkov I know is late, but setting environment var NODE_PRESERVE_SYMLINKS to 1 should work (only node 7.1 or newer).

mocha -r ts-node/register --preserve-symlinks ./src/**/*.test.ts

this worked for me.

I have the code (./src) in a place and a workspace in other location (which contains node_modules), ie:

  • {real_place}
    | -src

///////////////////////

  • workspace
    | - node_modules/
    | - src/ --> {real_place}/src

running the script with --preserve-symlinks fix the missing module error. (chai in my case)

Symlink issues are likely fixed in 9.0.0 by #970

Was this page helpful?
0 / 5 - 0 ratings