Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS My file in nodejs with express don´t work

Belan

Coder
Hello!

When I execute the code nodejs with npm start this message apear.

(node:3900) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use node --trace-warnings ... to show where the warning was created)
C:\Users\ar\ntask-api\index.js:1
import express from 'express';
^^^^^^

SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:77:18)
at wrapSafe (node:internal/modules/cjs/loader:1288:20)
at Module._compile (node:internal/modules/cjs/loader:1340:27)
at loader (C:\Users\ar\ntask-api\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.<computed> [as .js] (C:\Users\ar\ntask-api\node_modules\babel-register\lib\node.js:154:7)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Function.Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at Object.<anonymous> (C:\Users\ar\ntask-api\node_modules\babel-cli\lib\_babel-node.js:154:22)
at Module._compile (node:internal/modules/cjs/loader:1376:14)

Node.js v21.2.0
 
It´s giving this wrong, but it´s better before.

node:internal/modules/esm/resolve:853
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'express' imported from C:\Users\ar\ntask-api\index.js
at packageResolve (node:internal/modules/esm/resolve:853:9)
at moduleResolve (node:internal/modules/esm/resolve:910:20)
at defaultResolve (node:internal/modules/esm/resolve:1130:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
at link (node:internal/modules/esm/module_job:84:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
 
package.json
JSON:
{
  "name": "ntask-api",
  "version": "1.0.0",
  "description": "API de gestão de tarefas",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "babel-node index.js"
  },
  "author": "",
  "dependencies": {
    "babel-cli": "7.0.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.5.0"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.23.3"
  }
}

index.js
JavaScript:
import express from 'express';
const PORT = 3000;
const app = express();
app.get("/", (req,res) => res.json({status: "NTASK API"}));
app.listen(PORT, () => console.log(`NTASK API - porta ${PORT}`));
 
Last edited by a moderator:
I try to install but to apear this

Code:
npm install express
npm ERR! code ETARGET
npm ERR! notarget No matching version found for [email protected].
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.

npm ERR! A complete log of this run can be found in: C:\Users\ar\AppData\Local\npm-cache\_logs\2023-12-05T22_03_46_324Z-debug-0.log

there is another version of express or [email protected]
 
Last edited by a moderator:

New Threads

Buy us a coffee!

Back
Top Bottom