Yêu cầu thg 4 5, 2021 3:13 SA 8186 0 6
  • 8186 0 6
+3

[Nodejs, Typescript] Lỗi: Cannot use import statement outside a module

Chia sẻ
  • 8186 0 6

Mình vừa thêm 1 vài file .ts vào dự án nodejs, thì không npm start được (build tsc thì ok không lỗi gì)

Lỗi: Cannot use import statement outside a module

package.json node: 14.15.0

tsconfig.json

{
    "compilerOptions": {
        "target": "ES2016",
        "module": "commonjs"
    }
}

Bạn nào biết lỗi chỉ giúp mình với, thank you all.

thg 4 5, 2021 3:15 SA

Bác cho ae xin cái script của alias "start" trong file package.json với được hơm?

thg 4 5, 2021 3:22 SA

"scripts": { "start": "node bin/www" }

cái này à b

6 CÂU TRẢ LỜI


Đã trả lời thg 4 5, 2021 11:45 SA
+2

Update

Mình đã fix được lỗi, là do có 1 file .js có sử dụng cú pháp import ... from (cái này chỉ dùng trong file .ts), mà đúng ra là phải dùng require().

Thank you all

Chia sẻ
Avatar Trần Xuân Thắng @tranxuanthang
thg 4 6, 2021 6:42 SA

Nodejs từ lâu đã sử dụng được import kiểu ES module rồi mà nhỉ. Bạn thử xem "type": "module" đã có trong package.json chưa?

https://nodejs.org/api/esm.html#esm_modules_ecmascript_modules

thg 4 7, 2021 2:16 SA

@tranxuanthang nếu vậy thì cần đổi đuôi file .js sang .mjs anh à

Đã trả lời thg 4 5, 2021 3:23 SA
0

Ở file package.json bạn thêm "type": "module" xem sao ạ

Chia sẻ
thg 4 5, 2021 3:29 SA

Thêm cái này vào thì nó lại báo file bin/www không có đuôi file (trước giờ ko có đuôi file .js vẫn chạy ok đấy). T sửa thành www.js thì lại báo lỗi khác liên quan đến import. sao ra nhiều lỗi vậy nhỉ, từ khi thêm mấy file .ts này vào

Avatar Thái Viết Phúc @qtwyefqywewqvehqw
thg 4 9, 2021 3:02 SA
Đã trả lời thg 4 5, 2021 3:27 SA
0
const path = require('path');
const isProduction = false;
const mode = isProduction ? 'production' : 'development';
const devtool = isProduction ? false : 'inline-source-map';
module.exports = {
  entry: './server/server.ts',
  target: 'node',
  mode,
  devtool,

  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
          }
        ]
      },
      {
        test: /\.ts$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
          },
          {
            loader: 'ts-loader',
          },
        ]
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.ts', '.json' ]
  },
  output: {
    filename: 'server.js',
    path: path.resolve(__dirname, 'build'),
    library: 'server',    // very important line
    libraryTarget: 'umd',    // very important line
    umdNamedDefine: true     // very important line
  },
  node: {
    __dirname: false,
    __filename: false,
  },
};
Chia sẻ
thg 4 5, 2021 3:35 SA

check chatwork đi a, e nhờ

Đã trả lời thg 4 5, 2021 8:47 SA
0

Có thể lib bạn đang dùng đã hỗ trợ pure ESM nên mới bị lỗi như thế.

Chia sẻ
Đã trả lời thg 4 5, 2021 9:36 SA
0
{
        "include": [
		"src/**/*.ts"
	],
	"lib": [
		"esnext",
		"dom"
	]
}

Bạn thử thêm cái này vào tsconfig.json

Chia sẻ
Đã trả lời thg 11 20, 2021 9:58 SA
0

mình cũng gặp lỗi tương tự do dùng cú pháp import .... from, mình đổi sang require() là fix đc luôn. Mình tham khảo câu trả lời ở bài này https://quizdeveloper.com/faq/syntaxerror-cannot-use-import-statement-outside-a-module-in-nodejs-aid2327

Chia sẻ
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí