10

I'm trying to use the paths property in tsconfig.json in a NodeJS project.

In tsconfig.json I have something like this:

"baseUrl": ".",
"paths": {
  "@myApp/server/*": [
    "server/src/*"
  ],
  "@myApp/common/*": [
    "common/src/*"
  ]
},

Running tsc outputs all JS files as expected, but they retain the @myApp... imports. As a result, node won't run as it can't resolve all modules having as path @myApp....

I can't find a way to convert the paths I've set in tsconfig.json to a value that can be used by node. I've only found this question on SO, but it's quite outdated and it does not lead to a clean solution.

Do we have a way to transpile TS to JS in a way in which we are able to use paths?

6
  • why you are using baseUrl to "../" instead "." Commented Aug 4, 2018 at 9:11
  • @AvinashMaurya typo, I've edited the question :-) Commented Aug 4, 2018 at 9:19
  • And I am sure that you are aware of the fact that baseUrl is relative to the location of 'tsconfig.json'. can you please confirm? Commented Aug 4, 2018 at 9:23
  • Yes of course. As I said, typescript is working fine, the setup in tsconfig.json is correct. The problem is that the absolute paths defined in tsconfig.json dont't work in JS, and I'm looking for a way to convert them to relative paths. Commented Aug 4, 2018 at 9:26
  • what is the command that you are using to transpile ts code? Commented Aug 4, 2018 at 9:41

4 Answers 4

13

If anyone out here is using with nodejs and tsconfig-paths. You can use the following commands to make the absolute paths work:

// With ts-node    
ts-node -r tsconfig-paths/register src/main.js

// With node    
node -r ts-node/register/transpile-only -r tsconfig-paths/register dist/main.js

Got this from here: https://github.com/dividab/tsconfig-paths/issues/61

Sign up to request clarification or add additional context in comments.

Comments

2

Until today I was using tspath as suggested in the accepted answer, but I started to get some issues with relative paths having as baseUrl: '../'.

I switched to a webpack based approach, with this webpack.config.ts config:

import { TsConfigPathsPlugin } from 'awesome-typescript-loader';
import * as fs from 'fs-extra';
import { join } from 'path';
import * as webpack from 'webpack';

const packageConfig = fs.readJSONSync('./package.json', { encoding: 'utf-8' });

const externals = {};
for (const packageName in packageConfig.dependencies)
  externals[packageName] = packageName;

const serverConfig: webpack.Configuration = {
  entry: {
    index: './src/index.ts'
  },
  resolve: {
    extensions: ['.ts', '.js'],
    plugins: [
      new TsConfigPathsPlugin({ configFileName: 'tsconfig.json' })
    ]
  },
  target: 'node',
  node: {
    __dirname: false
  },
  externals,
  output: {
    path: join(__dirname, 'dist'),
    filename: '[name].js',
    library: '[name]',
    libraryTarget: 'umd'
  },
  module: {
    rules: [{
      test: /\.ts$/,
      loader: 'awesome-typescript-loader'
    }]
  },
  plugins: [
    new webpack.BannerPlugin(`Copyright © 2018-${new Date().getFullYear()} BOHR. All rights reserved.`)
  ],
  mode: 'production',
  optimization: {
    minimize: false
  }
};

// tslint:disable-next-line:no-default-export
export default [serverConfig];

Note the imports, you will need to add few packages to use this configuration.

Comments

1

I've found this npm package that converts all absolute paths to relative paths: https://www.npmjs.com/package/tspath.

Running tsc will produce the files with the absolute paths (e.g. @myApp/server/my-library). Then run tspath will convert all paths to the relative path.

Comments

-1

Why won't you just use the path module in the code and resolve the path to rest of the directories relative to the one where you started the server from?

Take a look at the line 6 in this example: https://github.com/Farata/angulartypescript/blob/master/code-samples/Angular6/chapter12/server/rest-server-angular.ts.

The __dirname points at the directory where the server was started from, and public is a subdirectory of this directory. I didn't use the paths option of tsc.

1 Comment

Take a look at this post, goenning.net/2017/07/21/… :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.