0

I try to import javascript-code into my express server to be used, but get the follwing error when starting the express server with

node TestProxy.js

Error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\develop\mytestproject\src\proxy\testcode' imported from C:\develop\mytestproject\src\proxy\TestProxy.js
Did you mean to import ../testcode.js?
←[90m    at new NodeError (node:internal/errors:372:5)←[39m
←[90m    at finalizeResolution (node:internal/modules/esm/resolve:437:11)←[39m

I have the following setup:

My Server (TestProxy.js) looks like this:

const express = require('express')
const app = express()

import { testMethod } from "./testcode";

app.get('/myrequest', function(req, res){
    testMethod
})

app.listen(3002)

My javascript code (testcode.js) looks like this:

export const testMethod = () => {
    console.debug('in testMethod!')
}

My Project structure:

under src I have:

enter image description here

My package.json:

{
  "name": "myname",
  "type": "module",
  "version": "1.0.0",
...
...
  "devDependencies": {
...
...
    "webpack": "^5.73.0",
    "webpack-cli": "^4.10.0",
    "webpack-dev-server": "^4.9.2"
  },
  "dependencies": {
...
...
    "express": "^4.18.1",
...
...
  }
}
mcfly soft
  • 11,289
  • 26
  • 98
  • 202
  • 2
    ```import { testMethod } from "./testcode";``` to ```import { testMethod } from "./testcode.js";``` – Kiddo Oct 03 '22 at 12:43
  • 1
    The error says `../testcode.js` but you say the code is `./testcode.js` which is it? – evolutionxbox Oct 03 '22 at 12:44
  • @evolutionxbox that's just Node's stupidly formatted suggestion, I have no idea why someone thought that prefixing a suggested filename with `../` would be a good idea. What it means to say is exactly what @Dazil is suggesting: add an extension. See [this issue](https://github.com/nodejs/node/issues/38484#issuecomment-830400600). – robertklep Oct 03 '22 at 12:49
  • I am facing same problem ```require``` doesn't need file extension but ```import``` needs wilt extension. – Kiddo Oct 03 '22 at 13:18

0 Answers0