1

I am trying to figure this out for hours but i can not solve this seemingly easy problem.

I want to write an nodejs app with typescript. I want to use the import statement like import express from 'express'; because then I get autocomplete in visual studio code. With require this does not work.

How can i export a function? What standard should i use? es6, commonjs,...?

How should i configure my tsconfig.json?

//tsconfig.json

{
  "exclude": ["node_modules"],
  "compilerOptions": {
    "target": "ES6" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
    "module": "ES6" /* Specify what module code is generated. */,
    "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
    "outDir": "dist" /* Specify an output folder for all emitted files. */,
    "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
    "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
    "strict": true /* Enable all strict type-checking options. */,
    "skipLibCheck": true /* Skip type checking all .d.ts files. */
  }
}

1 Answer 1

2

I assume that you already have nodejs installed on your computer/server. I did these steps this morning, created a new node project with typescript. You can of course choose other versions as you like.

  1. Run command npm init -y

  2. Run command npm install --save-dev typescript

  3. Create tsconfig.json

  4. add code to tsconfig.json

    { "compilerOptions": { "module": "commonjs", "esModuleInterop": true, "target": "es6", "moduleResolution": "node", "sourceMap": true, "outDir": "dist" }, "lib": ["es2015"] }

  5. Run command npm install --save [email protected]

  6. Run command npm install -save-dev @types/[email protected]

  7. Create app.ts in src folder

  8. Add code to app.ts

    import express from 'express'; const app = express(); const port = 3000;

    app.get('/', (req, res) => { res.send('Hello World!'); });

    app.listen(port, () => { return console.log(Express is listening at http://localhost:${port}); });

  9. Update package.json with "main": "dist/app.js"

  10. Run command to compile ts code npx tsc

  11. Run command node dist/app.js to execute program

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

3 Comments

Thanks for the reply. This throws than an error ReferenceError: exports is not defined in ES module scope. I use in my typescript files no require anymore. Just es6 export and import. So i have to set the compiler option to : "module": "es6", , or am I wrong? After that I get the error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\ticketing\dist\routes\admin' imported from C:\ticketing\dist\app.js The compiled app.js has this line: import adminRouter from './routes/admin';
According to this answer, yes https://stackoverflow.com/a/43999062/1349916. Try to create a new project, follow my steps and then compare with your old project.
I fixed it. I changed import { createOrder } from '../paypal/paypal'; to import { createOrder } from '../paypal/paypal.js';

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.