最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node Error: Cannot use import statement outside a module even though I'm not - Stack Overflow

programmeradmin2浏览0评论

I am using Pm2 and this is the error:

SyntaxError: Cannot use import statement outside a module

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

The issue is, the package.json is already set to "type": "module".

Also, everything used to work fine until I restart the server.

Here is the actual .js file:

const http = require('http');
const url = require('url');
const querystring = require('querystring');
    
const hostname = 'localhost';
const port = 8080;
    
import captureWebsite from 'capture-website';


const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
    
    ....
});

I am using Pm2 and this is the error:

SyntaxError: Cannot use import statement outside a module

Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

The issue is, the package.json is already set to "type": "module".

Also, everything used to work fine until I restart the server.

Here is the actual .js file:

const http = require('http');
const url = require('url');
const querystring = require('querystring');
    
const hostname = 'localhost';
const port = 8080;
    
import captureWebsite from 'capture-website';


const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
    
    ....
});
Share Improve this question edited Nov 18, 2021 at 23:17 Ivan Shatsky 15.6k2 gold badges25 silver badges50 bronze badges asked Nov 18, 2021 at 23:13 jefiwojefiwo 1091 gold badge1 silver badge7 bronze badges 4
  • 1 @fast-reflexes Can you give a code example? Not too familiar with Node. – jefiwo Commented Nov 18, 2021 at 23:24
  • 1 @fast-reflexes Hmm, it says those are fine as they are though. The error is only with import captureWebsite from 'capture-website'; – jefiwo Commented Nov 18, 2021 at 23:28
  • 1 @fast-reflexes That's fine and all, but the error is import captureWebsite from 'capture-website';. That line is what is causing the error. Can we just focus on that please – jefiwo Commented Nov 18, 2021 at 23:37
  • Node is just javascript. – Parking Master Commented Nov 19, 2021 at 0:36
Add a ment  | 

1 Answer 1

Reset to default 3

If the require calls are not throwing an error, the "actual .js file" creating the http server in the post is being treated as CommonJS. But CommonJS code can't use import statements and needs to use import expressions instead (see node and MDN docs).

If you use dynamic imports ( which are asynchronous) you would also need to use the imported module after an await statement (inside an async function), or in a success handler provided in a then method:

// ....

import('capture_website')
.then( capture_website => {
      // code requiring existence of `capture_website`
 })
.catch( err=> console.error("Import of capture_website failed", err));

However you can import CommonJS modules using an import statement in ES6 files (Node doc).

Hence a better solution may be to rename the main js file posted to give it a .mjs extension, and replace all the require statements in the file with import statements:

import http from 'http';
import url from 'url';
import querystring from 'querystring';

This gets rid of the Cannot use import statement outside a module syntax error. Imported modules that are CommonJS should still be able to require modules using the require function.

发布评论

评论列表(0)

  1. 暂无评论