Home > database >  Why isn't my module in the otuput folder?
Why isn't my module in the otuput folder?

Time:09-22

Consider these files:

mymodule.mjs

export const answer = 42

tsconfig.json

{
  "compilerOptions": {
      "rootDir": "src",
      "outDir": "lib",
      "baseUrl": ".",
      "skipLibCheck": true,
      "target": "es6",
      "module": "commonjs",
      "moduleResolution": "node",
      "sourceMap": true,
      "declarationMap": true,
      "inlineSources": false,
      "declaration": true,
      "stripInternal": true,
      "lib": [
        "es2016",
        "dom"
      ],
      "strict": true,
      "noImplicitReturns": true,
      "noUnusedLocals": true
  },
  "include": [
      "src"
  ]
}

the package.json:

{
  "name": "deleteme2",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "@types/jest": "^26.0.15",
    "@types/node": "^12.0.0",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "4.0.3",
    "typescript": "^4.1.2",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "compile": "tsc", 
    "build": "yarn run compile",
    "start": "node lib/server.mjs",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

It compile sucessfully with:

$ yarn run build

However, when I try to start it yarn run start I get the error:

$ node lib/server.mjs
internal/modules/cjs/loader.js:892
  throw err;
  ^

Error: Cannot find module 'C:\Users\user1\Desktop\deleteme2\lib\server.mjs'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
    at Function.Module._load (internal/modules/cjs/loader.js:745:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
error Command failed with exit code 1.

Why isn't the server.mjs in the output folder?

CodePudding user response:

Because you're trying to compile .mjs file with tsc.

tsc only supports .ts,.tsx files since it's meant to be used to compile TypeScript to JavaScript only. So you can not use it to compile .mjs,.js or any other file types.

If you change your file to mymodule.ts and put it inside src/ (because you only include src in your tsconfig.json), then it should work as expected. In this case the output file will be in .js, so you need to run node lib/myModule.js instead.

  • Related