Typescript: Intellisense issue with NODE_PATH evnvironment variable

Created on 10 Aug 2016  路  11Comments  路  Source: microsoft/TypeScript

_From @lekhnath on August 9, 2016 18:33_

Steps to Reproduce:

Project Structure

<project-root>
  + libs
       - utils.js
  - app.js

utils.js
exports.greetings = function(msg){console.log(msg);}

app.js
var utils = require('libs/utils');
utils.greetings('Hello');

The problem is I am not getting intellinsense working properly but the program runs fine if run with :
NODE_PATH=./ node app

But the relative require require('./libs/utils') gives the intellisense.

_Copied from original issue: Microsoft/vscode#10359_

Question

Most helpful comment

Hi guys, I've tried "mapRoot": "./src/" instead of baseUrl and it helped. (VSC 1.7.1)

{
    "compilerOptions": {
        "target": "es6",
        "mapRoot": "./src/"
    }
}

And now in javascript file I can use absolute paths like:
import {isLoading} from 'reducers/competitions';

Note that reducers/competitions are located in ./src/reducers/competitions.

All 11 comments

To my knowledge the TS compiler and language server don't support this resolution pattern. Will move to the TS team.

You can use a jsconfig.json or tsconfig.json with the baseUrl option set to your project root. If you set your baseUrl to your project root (which is probably just "./"), then require("libs/utils") should resolve to projectRoot/libs/utils.

If you need more customization, you can use the "paths" option. See #5039 for more details.

placing a jsconfig.json file at project root with baseUrl set to ./ for compilerOptions still DIDN'T work.

Screenshot of not working :
no intellisense

Screenshot of working but with relative path:
has intellisense

Did you try putting baseUrl outside of the compilerOptions object?

Did you try putting baseUrl outside of the compilerOptions object?

outside compilerOptions it is ignored. the correct place is inside it

@lekhnath can you share a sample we can play with?

example.zip

@mhegazy I've attached minimal example.

Also, I'm getting 'Property baseUrl is not allowed` warning in VSCode when using in jsconfig.json

{
"compilerOptions": {
"target": "es6",
"baseUrl": "./"
}
}

Has anyone solved this? We're getting the same issue. Seems baseUrl definitely does not belong inside of compilerOptions at least in Code v1.7.1. While it appears to be accepted at the root of the config object, it isn't fixing this particular issue.

We set NODE_PATH when running and testing our apps. For example:

NODE_PATH=. node ./bin/www

so we can include local libs with sane path resolution:

const hashUtil = require('src/lib/hashUtil');

Cannot seem to get Code IntelliSense to work with this style using any baseUrl configuration. It's only working when explicitly requiring modules using a relative path.

(OS X 10.12.1)

Hi guys, I've tried "mapRoot": "./src/" instead of baseUrl and it helped. (VSC 1.7.1)

{
    "compilerOptions": {
        "target": "es6",
        "mapRoot": "./src/"
    }
}

And now in javascript file I can use absolute paths like:
import {isLoading} from 'reducers/competitions';

Note that reducers/competitions are located in ./src/reducers/competitions.

Yep mapRoot did it. Thanks @jurosh.

Is this solution still valid?
setting mapRoot seems to be throwing errors in tests.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

quantuminformation picture quantuminformation  路  273Comments

metaweta picture metaweta  路  140Comments

blakeembrey picture blakeembrey  路  171Comments

disshishkov picture disshishkov  路  224Comments

Taytay picture Taytay  路  174Comments