我不明白出了什么问题.节点v5.6.0 NPM v3.10.6
代码:
function (exports, require, module, __filename, __dirname) {
import express from 'express'
};
Run Code Online (Sandbox Code Playgroud)
错误:
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:140:18)
at node.js:1001:3
Run Code Online (Sandbox Code Playgroud) 我试图在我的项目中运行一些ES6代码,但我收到意外的令牌导出错误.
export class MyClass {
constructor() {
console.log("es6");
}
}
Run Code Online (Sandbox Code Playgroud) 尝试导入本地文件时出现错误,但在使用 npm 包时没有问题。
import express from 'express'
import next from 'next'
import apis from './src/server/api'
Run Code Online (Sandbox Code Playgroud)
export default {
ello: 'bye',
jamie: 'hey'
}
Run Code Online (Sandbox Code Playgroud)
node --experimental-modules --inspect server.js
Run Code Online (Sandbox Code Playgroud)
For help, see: https://nodejs.org/en/docs/inspector
(node:20153) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/esm/default_resolve.js:59
let url = moduleWrapResolve(specifier, parentURL);
^
Error: Cannot find module '/var/www/goldendemon.hutber.com/src/server/api' imported from /var/www/goldendemon.hutber.com/server.js
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:59:13)
at Loader.resolve (internal/modules/esm/loader.js:70:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:143:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:43:40)
at link (internal/modules/esm/module_job.js:42:36) {
code: 'ERR_MODULE_NOT_FOUND'
}
Run Code Online (Sandbox Code Playgroud) 从版本8开始,我可以在Node.js中使用ES6模块语法吗?在这个网站上已经提出过类似的问题,但是答案已经过时了.我想知道Node.js的新版本情况是否已经改变了?
我使用 Vite via 创建了一个新的 Vue 应用程序npm init vue@latest。我根据官方指南将TailwindCSS添加到项目中。
运行npm run lint抛出错误
错误“模块”未定义 no-undef
因为它希望postcss.config.js和tailwind.config.js成为 ES 模块(我认为)。
当从postcss.config.js转换时
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
Run Code Online (Sandbox Code Playgroud)
到
export const plugins = {
tailwindcss: {},
autoprefixer: {},
};
Run Code Online (Sandbox Code Playgroud)
和tailwind.config.js来自
module.exports = {
content: ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"],
theme: {
extend: {},
},
plugins: [],
};
Run Code Online (Sandbox Code Playgroud)
到
export const content = ["./index.html", "./src/**/*.{vue,js,ts,jsx,tsx}"];
export const theme = { …Run Code Online (Sandbox Code Playgroud) 我是 JS 和 Node 的新手。从我读到的内容来看,Node似乎不支持 ES6 导入,所以我被迫使用实验模式(将我的文件重命名为 mjs)
除非 mjs 文件需要使用require. 我当前的解决方案是将依赖于单独 .js 文件的 require 的代码块分解并导入该 js 文件
foo.mjs
import readFileSync from './util.js'
...
//orignally just do require('fs') and do fs.readFileSync()
const configFile = readFileSync(configFilePath);
Run Code Online (Sandbox Code Playgroud)
实用程序.js
const fs = require('fs');
const path = require('path');
function readFileSync(filePath) {
console.log(__dirname)
console.log(path.resolve(__dirname, filePath))
return fs.readFileSync(path.resolve(__dirname, filePath), 'utf8');
}
module.exports = readFileSync
Run Code Online (Sandbox Code Playgroud)
有什么办法可以避免这样做吗?我的 app.js 有很多require,我不想将所有这些部分分解成单独的 js 文件。
我尝试更改导入要求
// const express = require('express');
import * as express …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的应用程序节点后端更新为打字稿,但我一直遇到此语法错误:
/Users/Shared/website/src/server.ts:1
(function (exports, require, module, __filename, __dirname) { import *
as express from 'express';
^
SyntaxError: Unexpected token *
Run Code Online (Sandbox Code Playgroud)
我的 tsconfig/webpack/server 等设置如下:
服务器.ts
import * as express from 'express';
import * as path from 'path';
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', function(req, res, next){
res.sendFile(path.resolve(__dirname, '/public', 'index.html'));
});
app.listen(port, () => console.log(`Listening on port ${port}!`));
Run Code Online (Sandbox Code Playgroud)
webpack.config.json:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const CopyWebpackPlugin = …Run Code Online (Sandbox Code Playgroud) 我在 中定义了一个更漂亮的配置prettier.config.js。
"type": "module"添加到 package.json后(以启用在节点中使用导入/导出语法),运行 prettier 失败并出现以下错误:
Checking formatting...
[error] Invalid configuration file `prettier.config.js`: Must use import to load ES Module: /your/project/path/prettier.config.js
[error] require() of ES modules is not supported.
[error] require() of /your/project/path/prettier.config.js from /your/project/path/node_modules/prettier/third-party.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
[error] Instead rename prettier.config.js to end in .cjs, change …Run Code Online (Sandbox Code Playgroud) import functions from 'firebase-functions';
import UtilModuler from '@utilModuler'
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
Run Code Online (Sandbox Code Playgroud)
从 '@utilModuler' 导入 UtilModuler;^^^^^^^^^^
语法错误:Module._compile 处的意外标识符 (internal/modules/cjs/loader.js:721:23)
注意事项
我正在使用通过导入/导出编写的第三方库(@utilModuler)。可能的解决方法:
问题:有没有办法在谷歌云功能中使用混合导入 cjs 和 esm?(除了我上面描述的选项)
在部署功能中使用类似--experimental-modules会很好
我想导入package.json到test.js,两个文件都在同一目录中。
我尝试过require:
const jsonfile = require("./packages.json");
console.log({ jsonfile });
Run Code Online (Sandbox Code Playgroud)
它抛出错误:
file:///home/.../test.js:1
const jsonfile = require("./packages.json");
^
ReferenceError: require is not defined
at file:///home/.../test.js:1:18
at ModuleJob.run (internal/modules/esm/module_job.js:145:37)
at async Loader.import (internal/modules/esm/loader.js:182:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
Run Code Online (Sandbox Code Playgroud)
这个错误意味着,就像它在浏览器中运行一样,其中 no require,我找到了带有类似消息的答案。
我尝试过import:
import * as jsonfile from './packages.json';
console.log({ jsonfile });
Run Code Online (Sandbox Code Playgroud)
internal/process/esm_loader.js:74
internalBinding('errors').triggerUncaughtException(
^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/.../packages.json' imported from /home/.../test.js
at finalizeResolution (internal/modules/esm/resolve.js:271:11)
at moduleResolve (internal/modules/esm/resolve.js:694:10)
at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:784:11) …Run Code Online (Sandbox Code Playgroud) javascript ×8
node.js ×7
ecmascript-6 ×4
es6-modules ×3
webpack ×2
babel ×1
commonjs ×1
eslint ×1
json ×1
npm ×1
prettier ×1
tailwind-css ×1
typescript ×1
vue.js ×1