0
votes

I'm working on a nodeJs/Typescript app and I would like to be able to launch it from the dist/ folder.

The structure of each src and dist folders is something like that :

server/
|---dist/
|   |---server/
|       |---src/
|           |---index.js
|           |---utils/
|               |---connection.js
|---src/
|   |---index.ts
|   |---utils/
|       |---connection.ts
|
|---package.json
|---tsconfig.json

This dist folder is the generated code, with this path being configured using outDir inside of my tsconfig.json file.

I have also configured the main property in my package.json to be dist/server/src/index.js and the type property to be module

In this project, I am using Typescript 4.0.5

I have specified paths property in my tsconfig.json to be "utils/*" : ["src/utils/*"], the baseUrl property to be './' and the property moduleResolution strategy to be node

Now this is my issue. I would like to be able to import my modules the same way i do in my source code, but this time directly from the dist folder, like this :

import createConnection from 'utils/connexion';

However, when i hover the import declaration in VScode, it does not seems to resolve the module and i get an error Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'utils' imported from /home/maxime/Dev/JeuxDuPlacard/packages/server/dist/server/src/index.js When i do the same operation from my Typescript files, it works as expected.

The paths of the files in the dist folder is the same in the Server folder. If the problem comes form the module resolution, Is there a way to persist the paths aliases in the dist folder (without installing a third party library) ?

{
    "compilerOptions": {
        "esModuleInterop": true,
        "target": "es2020",
        "moduleResolution": "node",
        "sourceMap": true,
        "baseUrl": "./",
        "outDir": "dist",
        "strict": true,
        "lib": ["ES2017", "ES2020"],
        "types": [
            "node"
        ],
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "resolveJsonModule": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "incremental": true,
        "skipLibCheck": true,
        "strictPropertyInitialization": false,
        "paths": {
            "utils/*": ["src/utils/*"]
        }
    }
}

Thanks

1

1 Answers

0
votes

That's right, because you CAN'T import files from outside your workdir. What you could do, is you could link a local module and access it as you would any other npm package.

npm link (in package dir)
npm link [<@scope>/]<pkg>[@<version>]
alias: npm ln

https://docs.npmjs.com/cli/v6/commands/npm-link