TypeStrong / ts-node

TypeScript execution and REPL for node.js
https://typestrong.org/ts-node
MIT License
12.93k stars 531 forks source link

Ts-node fails when using symlinks #396

Closed mtfranchetto closed 4 years ago

mtfranchetto commented 7 years ago

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 git@github.com:tierratelematics/prettygoat.git git clone -b 4.0.0-alignment git@github.com: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

tlaziuk commented 7 years ago

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
k8w commented 7 years ago

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
k8w commented 7 years ago

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.

k8w commented 7 years ago

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

kirillgroshkov commented 6 years ago

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

blakeembrey commented 6 years ago
node -r ts-node/register --preserve-symlinks script.ts
j-oliveras commented 6 years ago

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

mauril26 commented 5 years ago

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:

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

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

cspotcode commented 4 years ago

Symlink issues are likely fixed in 9.0.0 by #970

holylander commented 3 years ago

--preserve-symlinks

this setting did the job while using ts-node-dev. Thanks a lot @k8w