1

My file fetch.js:

import fetch from 'node-fetch'
url = "https://www.google.com"

const getTours = async (url) => {
    try {
        const resp = await fetch(url)
        console.log("fetch", resp)
        return resp
    }
    catch(error){
        console.log(error)
    }
}

getTours(url)

I did in terminal:

npm init
npm install node-fetch
node fetch.js

I got as a result:

/home/yan/Desktop/Node Tutorial/fetch.js:1
(function (exports, require, module, __filename, __dirname) { import fetch from 'node-fetch'
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)

I tried to replace the import with a require as such:

const fetch = require('node-fetch')
url = "https://www.google.com"

const getTours = async (url) => {
    try {
        const resp = await fetch(url)
        console.log("fetch", resp)
        return resp
    }
    catch(error){
        console.log(error)
    }
}

getTours(url)

but I got a similar error:

/home/yan/Desktop/Node Tutorial/node_modules/node-fetch/src/index.js:9
import http from 'node:http';
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/home/yan/Desktop/Node Tutorial/fetch.js:1:77)

Can you tell me what is going on ? I'm following some tutorials who have the same code and are running with no errors.

4 Answers 4

1

The similar error is related to the first, but for a different package: node-http

Try this:

const NodeHttp = require('node-http');

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

3 Comments

Hi, I did as you told but I still have the same error: Unexpected token import for import http from 'node:http'; I guess this line is called by the line const fetch = require("node-fetch")
The error is not coming from the fetch.js file but an index.js file. Do you have any file like that? @YannickPezeu
Yes I have a file like that but I guess it has been created in the node module when I ran npm install node-fetch It looks like my computer does not recognize the "import" keyWord
0

In your "package.json" add "type":"module" and also declare url using const.

{
  "main": "fetch.js",
  "type": "module",
}

import fetch from "node-fetch";
const url = "https://www.google.com"

const getTours = async (url) => {
 try {
     const resp = await fetch(url)
     console.log("fetch", resp)
     return resp
  } 
  catch(error){
    console.log(error)
  }
 }

 getTours(url)

Output

3 Comments

Hi, I did as you told but it does not help. Here is my package.json: { "name": "node-tutorial", "version": "1.0.0", "description": "", "main": "fetch.js", "type": "module", "dependencies": { "node-fetch": "^3.1.0", "nodemon": "^2.0.14" }, "devDependencies": { "@babel/cli": "^7.16.0", "@babel/core": "^7.16.0", "@babel/node": "^7.16.0", "@babel/preset-env": "^7.16.0" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
Hi, the url variable was also undeclared. have you fixed it?
Ray3du: Yes I did add the const as suggested: const url = "https://www.google.com"
0

You can use the esm module which allows to use either import or require.

First, install esm in your project running this:

$ npm install --save esm

Then, update your node start script to use esm with the following line:

node -r esm app.js

1 Comment

Hi, I did as you told and I get this error: yan@yan-VirtualBox:~/Desktop/Node Tutorial$ node -r esm fetch.js /home/yan/Desktop/Node Tutorial/node_modules/node-fetch/src/index.js:1 Error [ERR_INVALID_PROTOCOL]: Protocol 'node:' not supported. Expected 'file:' at Object.<anonymous> (/home/yan/Desktop/Node Tutorial/node_modules/node-fetch/src/index.js:1) at Generator.next (<anonymous>)
0

I could resolve this issue by upgrading my node version with nvm.

I have a node version of v8.17.0 I upgraded it to the LTS (long term support) of my current time which was v16.13.0 by running the code:

nvm install 16.13.0

To find your current LTS please follow this page: https://nodejs.org/en/

Comments

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.