Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to resolve absolute modules paths for node.js? #12954

Closed
pie6k opened this issue Dec 15, 2016 · 2 comments
Closed

How to resolve absolute modules paths for node.js? #12954

pie6k opened this issue Dec 15, 2016 · 2 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@pie6k
Copy link

pie6k commented Dec 15, 2016

I need modules to be resolved basing on baseUrl so output code is usable for node.js

this is my src/server/index.ts

import express = require('express');
import {port, databaseUri} from 'server/config';

...

and this is my src/server/config/index.ts

export const databaseUri: string = process.env.DATABASE_URI || process.env.MONGODB_URI;
export const port: number = process.env.PORT || 1337;

Running tsc I'm able to compile all files without erros, but output: dist/server/index.js is

"use strict";
var express = require("express");
var config_1 = require("server/config");

...

Resulting with Cannot find module 'server/config' if I'm trying to use it with node dist/sever/index.js.

Why server/config path is not resolved in any way so it would be possible to use compiled code or how to make it resolve it. Or what am I doing or thinking wrong way?

This is my tsconfig.json:

{
  "compileOnSave": true,
  "compilerOptions": {
      "baseUrl": "./src",
      "rootDir": "./src",
      "module": "commonjs",
      "target": "es5",
      "typeRoots": ["./src/types", ".node_modules/@types"],
      "outDir": "./dist"
  },
  "include": [
      "src/**/*"
  ],
  "exclude": [
      "node_modules",
      "**/*.spec.ts"
  ]
}

I was also looking for any example/starter/snippet/docs for typescript 2.0+ for node.js showing typescript configuration in action with no luck. Would be nice to have something like that.

Thanks.

My tsc --version is 2.1.4

Note I don't want to use ../../../../relative paths.

@pie6k pie6k changed the title How to resolve modules for node.js? How to resolve absolute modules paths for node.js? Dec 15, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Dec 15, 2016

The compiler does not rewrite module names. module names are "resource identifiers" and the compiler does not touch them, just like your string literals. all what baseUrl and paths do is tell the compiler where to find the .d.ts for this module, but no changes to the output are caused by these options.
you can find related discussion on why this is the case in #12597

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 15, 2016
@mlesk
Copy link

mlesk commented Feb 10, 2017

Considering this I fail to see the benefit of #5039 with respect to supporting absolute paths in a node environment. BaseUrl and paths configuration options do not assist with resolution of absolute path references in a typescript project's translated javascript files because ... these options are provided primarily to support tooling ? I will have to read through the use cases spelled out in 5039 to gain a better understanding of the motivations. I spent a bit of time fiddling around trying to get this to work due to my ignorance or obtuseness, not sure which one at the moment :-)

This leaves the options described here to be able to utilize absolute path references for imports that work in vscode tooling, typescript compilation, and at runtime within a node environment. For example, this will allow the original use case to be achieved:

project
└───src
     └───node_modules
           └───server
               └───config
                     └───index.ts

with this structure you can now import using absolute paths as you desired from anywhere within the src directory.

import {port, databaseUri} from 'server/config';

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants